javascript - 如何在 Vanilla Javascript 中不修改源数组的情况下找到数组的最后一个元素
问题描述
我有一个数组包含
const data = ['a', 'b', 'c', 'd'];
如何找到最后一个元素,结果应该是'd'
解决方案
使用函数slice
+解构赋值。
const data = ['a', 'b', 'c', 'd'],
[last] = data.slice(-1);
console.log(last);
推荐阅读
- r - hist3D 中的希腊字母
- r - 错误:软件包是由具有不同内部结构的 R 版本安装的;它需要重新安装才能与此 R 版本一起使用
- internationalization - 用阿拉伯语显示数字的正确方法
- python - 如何根据日期索引连接系列以形成 df 而不会丢失数据
- python - 如何使用 Selenium Webdriver 通过 xpath 查找?(Python)
- python - 在python中使用多个列表动态创建树图
- python-3.x - 类不调用 __init__
- vba - SumIf 语句 - 复杂的标准
- r - R ancova“如果错误”+“最小/最大警告”
- php - HTML 和 PHP 中的评论表单不显示