数组对象相同属性的相加 let obj = {}; const arr = [ { id: ‘aaa’, value: 11 }, { id: bbb,
value: 21 }, { id: ‘aaa’, value: 111 }, { id: bbb, value: 222 }, { id: ccc,
value: 22 }, { id: ddd, value: 122 }, { id: ccc, value: 22 }, { id: ddd, value:
122 }, { id: ccc, value: 22 }, { id: ddd, value: 122 }, ]; // 第一种方式 arr.forEach(
v => obj[v.id] = obj[v.id] === undefined ? v.value : obj[v.id] + v.value)
console.log(obj) // 第二种方法 arr.forEach(v=>{ if (obj.hasOwnProperty(v.id)) { obj[v
.id] += v.value } else{ obj [v.id] = v.value } }) console.log(obj) 数组去重 //1
reduce去重,针对数组对象 let person = [ {id: 0, name: "A"}, {id: 1, name: "B"}, {id: 2,
name: "C"}, {id: 3, name: "D"}, {id: 1, name: "E"}, {id: 2, name: "F"}, ]; let
obj= {}; let peon = person.reduce((cur,next) => { obj[next.id] ? "" : obj[next.
id] = true && cur.push(next); return cur; },[]) //设置cur默认类型为数组,并且初始值为空的数组
console.log(peon); //2 Set去重,针对纯数组元素 let arr = [1,2,2]; console.log([...new Set(
arr)]); // [1,2]