at
数组at方法(获取数组中特定位置的元素)
const arr = ['1M', '2M', '3M'];
console.log(arr.at(-1)) // 倒数第一个值
console.log(arr.at(-2)) // 倒数第二个值
console.log(arr.at(0)) // 正数第一个
console.log(arr.at(1)) // 正数第二个
copyWithin
数组copyWithin方法(方法用于从数组的指定位置开始拷贝元素,并将其粘贴到同一数组中的另一个指定位置。它不会改变数组的长度,会改变原数组。)
array.copyWithin(target, start, end)
接受三个参数。
target(必需):要替换元素的起始位置。如果为负值,表示倒数。
start(可选):开始复制元素的起始位置,默认为 0。如果为负值,表示从末尾开始计算。
end(可选):停止复制元素的结束位置(不包含),默认为数组长度。如果为负值,表示从末尾开始计算。
[1, 2, 3, 4, 5].copyWithin(0, 3) // [4, 5, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(0, 3, 4) // [4, 2, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(0, -2, -1) // [4, 2, 3, 4, 5]
日常工作中仅仅使用到很常用的数组方法,其实数组方法是非常庞大的,后续探索更多方法!
特定场景下使用还是比较方便的,copyWithin相比较于at的使用频次可能较低一些,根据不同的业务需求进行应用。