实际开发中常常要用到前端js对象数组去重,网上方法大同小异,然后有些确没有经过实践,现提供经过实践的方法:
js对象数组求补集:
var B=b;//数组B
let test=A.reduce(function(pre,cur){
if(B.every(item=>item.唯一键!==cur.唯一键)){
pre.push(cur)
}
return pre;
},[]);
test最后为补集,即:排除B在A中存在的
js对象数组去重:
//去重
let hash = {};
var newArr = 数组.reduceRight((item, next) => {
唯一键 ? '' : 唯一键 = true && item.push(next);
return item
}, []);
newArr为去重后的对象数组