js get the last element of the array All In One
js get the last element of the array All In One
Array.prototype.at()
✅
// Array.at()
const arr = [1, 2, 3, 4, 5];
let index = 1;
console.log(arr.at(index));
// 2
let lastIndex = -1;
console.log(arr.at(lastIndex));
// 5
// 等价于 👎
console.log(`\n`,arr[1]);
// 2
console.log(arr[arr.length - 1]);
// 5
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at
slice 👎
const arr = [1, 2, 3, 4, 5];
let lastIndex = -1;
console.log(arr.slice(lastIndex));
// [5]
console.log(arr.slice(lastIndex)[0]);
// 5
pop 🚀
const arr = [1, 2, 3, 4, 5];
console.log(arr.pop());
// 5
JavaScript & Array groupBy
All In One
Array
group
https://www.cnblogs.com/xgqfrms/p/15943811.html
refs
https://davidwalsh.name/array-prototype-at
https://flexiple.com/get-last-array-element-javascript/
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16403739.html
未经授权禁止转载,违者必究!