9.ES6-set数组去重

set去重

var demo = new Set([1,1,3,5,4,6]);
console.log([...demo]); // [1, 3, 5, 4, 6]

 合并两个数组,合并两个对象。

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}

改进

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

const obj1 = {
  a:1,
}
const obj2 = {
  b:1,
}
const obj = {...obj1,...obj2};//{a:1,b:1}

 

posted @ 2021-10-27 22:51  前端HL  阅读(85)  评论(0编辑  收藏  举报