哈喽,大家好,我是贼漂亮~今天聊聊这个数组新方法array.at!!!
这里有官方描述,可以瞅瞅 MDN描述
语法: arr.at(index)
参数: index可以为正数、负数,-1就会返回最后一个元素
大家都知道,当我们想要返回数组中某一个元素的时候我们一般会这么写:
const arr = ['one','two','three'];
console.log(arr[0]) // 'one'
通过数组中括号,在中括号中输入元素的下标来取这个元素,但是当我们想要取最后一个元素的时候,只能通过length-1
,当然如果元素非常少可以数的过来除外啦~
console.log(arr[arr.length -1]) // 'three'
使用array.at
就会很方便了。
const arr = ['one','two','three'];
console.log(arr.at(0)) // 'one'
const arr = ['one','two','three'];
console.log(arr.at(-1)) // 'three'
但是目前浏览器支持的还暂无哦~
ok~散会,PS贼漂亮在这里提前祝大家新年快乐!!!!!