作用一:
将多余的参数进行合并,转为数组
function test(a,...b){
console.log(a); //10
console.log(b); // [ "hello", "world" ]
}
test(10,'hello','world');
作用二:
将数组转成字符串
var a = [1, 2, 3, 4];
function test() {
console.log(...a); //1 2 3 4
}
test();
作用三:
将字符串转成数组
var a = 'abcd';
function test() {
console.log([...a]); //[ "a", "b", "c", "d" ]
}
test();
作用四:
合并数组
function test() {
let a = ['a','b','c'];
let b = ['d','e'];
let arr = [...a, ...b];
console.log(arr); //[ "a", "b", "c", "d", "e" ]
}
test();
作用五:
数组最大时修改数组数据类型传入Math.max得到最大值
function test() {
let a = [2,5,10];
console.log(Math.max(a)); //NaN
console.log(Math.max(...a)); //10
}
test();
拓展:
结合Set来进行数组去重
function test() {
var a = [1,1,3,4,1,6];
b=[...new Set(a)];
console.log(b); //[ 1, 3, 4, 6 ]
}
test();