4

I have a list of array items like this:

const items = [
  { a: 1 },
  { b: 2 },
  { c: 3 },
]

How can I return / log the last element: { c: 3 }

Here's what I've tried so far:

let newarray = items.map((item) => {
    console.log(item);
})

console.log(newarray);
KyleMit
  • 30,350
  • 66
  • 462
  • 664
baiju thomas
  • 146
  • 1
  • 2
  • 12
  • if you *really wanted* to use es6 `[last, ...others] = items.reverse(); console.log(last)` (don't do this) – Andrew Allen Mar 05 '20 at 15:10
  • 1
    Does this answer your question? [Destructuring to get the last element of an array in es6](https://stackoverflow.com/questions/33064377/destructuring-to-get-the-last-element-of-an-array-in-es6) – Andrew Allen Mar 05 '20 at 15:14

7 Answers7

26

Update 2021

You can use the Array.at() method, which was moved to Stage 4 in Aug, 2021

['a','b','c'].at(-1) // 'c'

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • Good answer. But misleading in terms, that it states that `Array.at()` is part of a living ECMAScript 2021 standard - whereas as of writing this comment (04.02.22) it's still a Stage 4 proposal. https://tc39.es/proposal-relative-indexing-method/#sec-array-prototype-additions – avalanche1 Feb 04 '22 at 10:36
  • Thanks @avalanche1 - updated the answer to clarify. Seems like stage 4 is a finished proposal and will be released whenever the next version of ECMAScript ships. – KyleMit Feb 04 '22 at 12:43
8

just log the length minus 1, nothing to do with es6:

console.log(items[items.length - 1])
bryan60
  • 28,215
  • 4
  • 48
  • 65
3

If your list has 3 items, the length is 3 but the last item index is 2, since arrays start at 0, so simply do this:

console.log(items[items.length - 1]);

Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Robouste
  • 3,020
  • 4
  • 33
  • 55
2

I want to let you try something different:

console.log(items.slice(-1));
MedaiP90
  • 302
  • 1
  • 4
  • 3
    items.slice(-1)[0] – alkhatim Jun 27 '21 at 09:34
  • In case the previous comment isn't clear, `slice()` returns an array. `items.slice(-1)` returns an array containing the last element of `items`. `items.slice(-1)[0]` is the element itself, i.e. the last item in `items`. – Paul Oct 22 '22 at 17:43
2

It's not required to use ES6 to perform the operation you're asking about. You could use either of the following:

/**
 * The last value in the array, `3`, is at the '2' index in the array.
 * To retrieve this value, get the length of the array, '3', and 
 * subtract 1. 
 */
const items = [1, 2, 3];
const lastItemInArray = items[items.length - 1] // => 3

or:

/**
 * Make a copy of the array by calling `slice` (to ensure we don't mutate
 * the original array) and call `pop` on the new array to return the last  
 * value from the new array.
 */
const items = [1, 2, 3];
const lastItemInArray = items.slice().pop(); // => 3

However, if you are dead set on using ES6 to retrieve this value we can leverage the spread operator (which is an ES6 feature) to retrieve the value:

/**
 * Create new array with all values in `items` array. Call `pop` on this 
 * new array to return the last value from the new array.
 *
 * NOTE: if you're using ES6 it might be a good idea to run the code
 * through Babel or some other JavaScript transpiler if you need to
 * support older browsers (IE does not support the spread operator).
 */
const items = [1, 2, 3];
const lastItemInArray = [...items].pop(); // => 3
kyle.stearns
  • 2,326
  • 21
  • 30
2

Update - October 2021 (Chrome 97+)

Proposal for Array.prototype.findLast and Array.prototype.findLastIndex is now on Stage 3!

You can use it like this:

const items = [
  { a: 1 },
  { b: 2 },
  { c: 3 },
];

const last_element = items.findLast((item) => true);
console.log(last_element);
NeNaD
  • 18,172
  • 8
  • 47
  • 89
  • This looks promising. But still doesn't support by Firefox. Do you have an idea how to mimic this with the existing ES6+ syntax? (With conditions like `item.is_active == 'yes'`) – Nipuna Jul 07 '22 at 05:32
  • 1
    Hi. Just can just do this `items.findLast((item) => item.is_active === 'yes');` – NeNaD Jul 07 '22 at 05:42
  • Yeah, I figured that. But the problem is, this isn't supported by Firefox and the method itself is relatively new so a bit older browsers won't support it. Seeking for something that would work on Firefox and legacy browsers. ES6+ is just fine. – Nipuna Jul 07 '22 at 06:20
  • This worked, `const activeKeys = items.map(item => item.is_active); const lastActiveIndex = activeKeys.lastIndexOf('yes')` – Nipuna Jul 07 '22 at 07:06
1

try this

console.log(items[items.length - 1]);
Max Ivanov
  • 133
  • 1
  • 9