js中合并数组对象并去重
靠对象属性的唯一性去重
cutarray(arr) {
let obj = {}; //obj用来记录该项重复出现了几次
let brr = []; //brr是去重后的数组
arr.forEach((item) => {
if (obj[item.good_sku_id] == undefined) {
obj[item.good_sku_id] = 1;
brr.push(item);
} else {
obj[item.good_sku_id] += 1;
}
});
return brr;
},
使用
let cartList = [
{ good_sku_id:1,name:'张'},
{ good_sku_id:2,name:'王'},
{ good_sku_id:1,name:'张'}
]
let newarr = this.cutarray(cartList)