三点运算符(…)的使用
作用(1):
它可以将多余的参数在行参进行合并,成为数组
function test(a , ...b){
console.log(a) //输出 1
console.log(b) //输出 ["GTA5" , "GTASA"]
}
test(1,'GTA5', 'GTASA')
作用(2):
可以将数组转成对应的字符串
var a = ["a","b","c","d"]
function test(){
console.log(...a) //输出 a b c d
}
test();
作用(3):
可以将字符串转成数组
var a = "abcd";
function test(){
console.log([...a]) //输出 ["a","b","c","d"]
}
test();
作用(4):
可以合并数组
function test(){
let a = [1,2,3];
let b = [4,5,6]
let arr1 = a.concat(b); //输出 [ 1, 2, 3, 4, 5, 6 ]
console.log(arr1);
let arr2 = [...a, ...b]; //输出 [ 1, 2, 3, 4, 5, 6 ]
console.log(arr2)
}
test();
作用(5):
数组最大值时修改数组数据类型传入Math.max得到最大值
function test(){
let a = [1,2,6];
console.log(Math.max(a)) //输出 NaN
console.log(Math.max(...a)) //输出 6
}
test();
拓展:结合Set 来进行 数组的去重
function test(){
var arr = [1,1,2,2,3,4,];
arr2 = [...new Set(arr)]
console.log(arr2) //输出 [1,2,3,4]
}
test();