1、组装对象或数组
数组
const color = ['red','yellow'];
const colorful = [...color,'green','pink'];
console.log(colorful);//[red,yellow,green,pink]
对象
const alp = {first:'a',second:'b'};
const alphabets = {...alp,third:'c'};
console.log(alphabets);//{first: "a", second: "b", third: "c"}
2、有时候我们想获取数组或对象除了前几项或者除了某几项的其他项
数组
const number =[1,2,3,4,5];
const [first,...rest] = number;
console.log(rest);// [2, 3, 4, 5]
对象
const user ={
username:'lux',
gender:'female',
age:20,
address:'beijing'
}
const {username,...rest1} = user;
console.log(rest1);//gender: "female", age: 20, address: "beijing"
3、对于对象(Object)而言,还可以用于组合成的新的Object。(ES2017 stage-2 proposal)当然如果有重复的属性名,右边覆盖在左边
const aa = {
a:1,
b:2,
c:3
}
const bb ={
c:5,
d:1
}
const total ={...aa,...bb};
console.log(total);//a: 1, b: 2, c: 5, d: 1
作者:陈嘻嘻啊
链接:https://www.jianshu.com/p/287e0bb867ae
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。