关于ES6的扩展运算符(es5合并数组或者合并对象)

2022-04-17  本文已影响0人  泪滴在琴上

ES5里面合并你可能会这么做:

const a = [1,2,3];
const b = [1,5,6];
const c = a.concat(b);//[1,2,3,1,5,6]

const obj1 = {
  a:1,
}
const obj2 = {
  b:1,
}
const obj = Object.assign({}, obj1, obj2);//{a:1,b:1}

在ES6里面通过扩展运算符:

const a = [1,2,3];
const b = [1,5,6];
const c = [...new Set([...a,...b])];//[1,2,3,5,6]   //new Set做了去重操作

const obj1 = {
  a:1,
}
const obj2 = {
  b:1,
}
const obj = {...obj1,...obj2};//{a:1,b:1}
上一篇下一篇

猜你喜欢

热点阅读