ES6扩展运算符...

ES6扩展运算符...

1.将一个数组转为用逗号分隔的参数序列。(若数组为空不产生任何效果)

console.log(1,...[2,3,4],5);
// 1,2,3,4,5

2.复制数组

var a = [1,2];
var b = [...a];
b.push(3);
console.log(b); // [1,2,3]
console.log(a);// [1,2],对原数组不会产生影响

3.合并数组

var a = [1,2];
var b = [3,4];
var c = [...a,...b];
console.log(c);//[1,2,3,4];
a.push(6);
console.log(a);//[1,2,6];
console.log(c);//[1,2,3,4]; //原数组变化,不影响合并之后的新数组

4.与解构赋值结合

var [a,...b] = [1,2,3,4];
console.log(a);//1
console.log(b)://[2,3,4]

5.将字符串分隔成数组

var a = [...'hello world'];
console.log(a);//["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]

6.Map和Set结构

// Map结构
var map = new Map([[1, 'a'],[2, 'b'],[3, 'c']]);
var arr = [...map.keys()];
console.log(map);//{1 => "one", 2 => "two", 3 => "three"}
console.log(arr);//[1,2,3]
// Generator 函数
var a = function*(){
          yield 'a';
          yield 'b';
          yield 'c';
     };
console.log([...a()]); // ['a','b','c'] 

你可能感兴趣的:(es6,javascript,前端,vue.js,开发语言)