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, 禁止轉載 🈲️,侵權必究⚠️!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章