扩展运算符
扩展运算符(spread)是三个点(...), 将一个数组转为用逗号分隔的参数序列 。
用法:
<script>
console.log(...[1,2,3]);
console.log(0,...[1,2,3],4,5);
function add(x, y) {
return x + y;
}
var numbers = [1, 2];
console.log(add(...numbers)); // 3
// 数组合并
let arr = [...[1,2,3],...[4,5,6]];
console.log(arr); //[1, 2, 3, 4, 5, 6]
// 与解构表达式结合
const [first, ...rest] = [1, 2, 3, 4, 5];
console.log(first, rest) //1 [2, 3, 4, 5]
//将字符串转成数组
console.log([...'hello']) //["h", "e", "l", "l", "o"]
</script>