Take last array object

Asked

Viewed 2,173 times

0

How can I always catch the last object of the array at the moment I’m using the following function but only takes the first one.

var IDs = arrayIds[0].id;

[
  {
    id: '1',
  },
  {
    id: '2',
  },
  {
    id: '3',
  }
]

1 answer

3


You can search by length using .length minus 1 that will always return the last item

let arr = [ '0', '1', '2', '3', '4', '5']


console.log(arr[arr.length -1])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.