数组对象去重reduce
最近刚好在项目中多次使用reduce解决一些数据问题,分享如下:
1.数组对象去重
arr= arr.reduce((cur,next) => {
obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
return cur;
},[])
2.相同某key,其他属性整合
this.datasr = this.datasr.reduce((cur, next) => { //有返回值
if (obj[next.year]) { //根据year属性整合
let o = cur.find((e) => next.year == e.year);
let oIndex = cur.findIndex((e) => next.year == e.year);
delete o.year;
o = { ...o, ...next };
cur.splice(oIndex, 1);
cur.push(o);
return cur;
} else {
obj[next.year] = true && cur.push(next);
}
return cur;
}, []);

浙公网安备 33010602011771号