当前位置:网站首页>Don't use array.length-1 to get the last item of the array

Don't use array.length-1 to get the last item of the array

2022-08-09 22:07:00 Maple moving forward

Please don't use array.length-1 to get the last item of the array. There is a more convenient and concise method: at , which is a method on the array prototype.

mdn:

The

at() method takes an integer value and returns the item at that index, positive and negative numbers are allowed.Negative integers count down from the last item in the array.

eg:

const a = [1,2,3];const lastItem = a.at(-1);console.log(lastItem ); // 3

Negative numbers start from -1 to represent the last digit.

原网站

版权声明
本文为[Maple moving forward]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091856036316.html