展开运算符 … 拆包
函数调用的体现
function f(a,b,c){}
var args = [1,2,3]
f(...args)
后面可以放表达式
let a = 1;
const arr = [
...(a>0? ['a']: [])
]
替代函数的apply方法
let arr1 = [1,2,3]
let arr2 = [4,5,6]
console.log(arr1.concat(arr2));
console.log(arr1);
Array.prototype.push.apply(arr1,arr2);
arr1.push(...arr2);
复制数组
const a1 = [1,2]
const a2 = a1;
console.log(a1 === a2);
const a3 = a1.concat()
console.log(a3);
console.log(a1 === a3);
const a4 = [...a1]
console.log(a4);
console.log(a4 === a1);
let arr = [{a:1},2,3]
let arr2 = JSON.parse(JSON.stringify(arr))
arr2[0].a = 2;
console.log(arr);
…打包
Array.from()
function get(a,b,c){
console.log(arguments);
console.log(Array.from(arguments));
}
get(1,2,3)
const copyArr = {
'0':'a',
'1':'b',
length:2
}
console.log(Array.from(copyArr));
Array.of()
console.log(Array.of(2,3,4,5));
console.log(Array(3).length);
console.log(Array(1,2,3));
console.log(Array.of(3));
copyWithin()
console.log([1,2,3,4,5].copyWithin(0,3));
find() ,findIndex()
let arr = [1,2,3,-1,-2,45]
let res = arr.find(function(i){
return i<0
})
console.log(res);
let resIndex = arr.findIndex(function(i){
return i<0
})
console.log(resIndex)