【问题标题】:How the get the last element in an array items using JavaScript如何使用 JavaScript 获取数组项中的最后一个元素
【发布时间】:2020-03-05 15:02:51
【问题描述】:

我有一个这样的数组项列表:

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

如何返回/记录最后一个元素:{ c: 3 }

这是我迄今为止尝试过的:

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

console.log(newarray);

【问题讨论】:

标签: javascript arrays ecmascript-6


【解决方案1】:

【讨论】:

  • 好答案。但在术语上具有误导性,它指出 Array.at() 是现行 ECMAScript 2021 标准的一部分——而在撰写此评论时 (04.02.22),它仍然是第 4 阶段的提案。 tc39.es/proposal-relative-indexing-method/…
  • 感谢@avalanche1 - 更新了答案以澄清。似乎第 4 阶段是一个已完成的提案,将在下一版 ECMAScript 发布时发布。
【解决方案2】:

只记录长度减1,与es6无关:

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

【讨论】:

    【解决方案3】:

    如果您的列表有 3 个项目,则长度为 3,但最后一个项目的索引为 2,因为数组从 0 开始,所以只需这样做:

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

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

    【讨论】:

      【解决方案4】:

      我想让你尝试一些不同的东西:

      console.log(items.slice(-1));
      

      【讨论】:

      • items.slice(-1)[0]
      【解决方案5】:

      试试这个

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

      【讨论】:

        【解决方案6】:

        不需要使用 ES6 来执行您询问的操作。您可以使用以下任一方法:

        /**
         * 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
        

        或:

        /**
         * 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
        

        但是,如果您对使用 ES6 检索该值一无所知,我们可以利用扩展运算符(这是 ES6 特性)来检索该值:

        /**
         * 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
        

        【讨论】:

          【解决方案7】:

          更新 - 2021 年 10 月(Chrome 97+)

          Proposal for Array.prototype.findLastArray.prototype.findLastIndex 现在处于第 3 阶段!

          你可以这样使用它:

          const items = [
            { a: 1 },
            { b: 2 },
            { c: 3 },
          ];
          
          const last_element = items.findLast((item) => true);
          console.log(last_element);

          【讨论】:

            猜你喜欢
            • 2019-10-19
            • 2019-09-10
            • 2022-06-28
            • 2020-07-03
            • 2023-04-08
            • 2022-09-29
            • 1970-01-01
            • 1970-01-01
            • 2012-01-28
            相关资源
            最近更新 更多