首页 > 解决方案 > 如何在 Vanilla Javascript 中不修改源数组的情况下找到数组的最后一个元素

问题描述

我有一个数组包含

const data = ['a', 'b', 'c', 'd'];

如何找到最后一个元素,结果应该是'd'

标签: javascriptarrayses2022

解决方案


使用函数slice+解构赋值。

const data = ['a', 'b', 'c', 'd'],
      [last] = data.slice(-1);

console.log(last);


推荐阅读