扩展运算符
是rest参数的逆运算,将一个数组转为用逗号分隔的参数数列
- 1、孤立数组元素
let iArray = ['1','2','3']
...iArray => 1 ,2,3
- 2、在数组中添加元素
let iArray = ['1','2','3']
let ['0',...iArray,'4'] => ['0','1','2','3','4']
- 3、在数组中删除元素(取出一个元素),与解构赋值结合,只能放在参数最后一位
const [first,...rest] = [1,2,3,4,5]
first --> 1
[...rest] = [2,3,4,5]
- 4、数组的合并
var arr1 = [0,1,2]
var arr2 = [3,4,5]
arr1.push(...arr2) ----> [0,1,2,3,4,5]
推荐使用下面的方法合并数组
var arr1 = [0,1,2]
var arr2 = [3,4,5]
[...arr1,...arr2] ----> [0,1,2,3,4,5]
- 5、字符串转化成数组
let str = "wasgd"
[...str] = ['w','a','s','g','d']
- 6、当做参数传递(和直接传数组的区别)
let arr = ['1','2','3']
hanshu(...arr)
hanshu(...arr){
console.log(...arr) ----> 1,2,3
}
网友评论