1.复制数组(真正的复制,效果等同于Array.from())
let arr = [1,2,3,4,5];
let arr2 = [...arr];
arr2.push(6);
console.log(arr);
console.log(arr2);
//返回结果
[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5, 6 ]
2.一般用法案例
let arr = [1,2,3,4,5];
console.log(...arr)
//返回结果
1 2 3 4 5
=============================================
let fun = (...arr) => {
console.log(arr)
}
fun(1,2,3,4,5)
//返回结果
[ 1, 2, 3, 4, 5 ]
=============================================
let fun = (a,b,c,d,e) => {
console.log(a,b,c,d,e)
}
fun(...[1,2,3,4,5])
//返回结果
1 2 3 4 5
=============================================
let fun = (a,b,...c) => {//注意这种情况下必须放在末尾
console.log(a,b);
console.log(c);
}
fun(1,2,3,4,5)
//返回结果
1 2
[ 3, 4, 5 ]
3.将类数组转换成数组
4.将字符串转换成数组
let str = 'welcome';
let arr = [...str];
arr.push('!');
console.log(arr)
//返回结果
[ 'w', 'e', 'l', 'c', 'o', 'm', 'e', '!' ]
网友评论