封装reduce根据某个属性对数组对象去重
2022-04-24 本文已影响0人
bryan_liu
function duplicateRemovalArr (arr, key) {
let hash = {};
let result = arr.reduce((total, currentValue) => {
if (!hash[currentValue[key]]) {
hash[currentValue[key]]= true;
total.push(currentValue);
}
return total;
},[]);
return result;
}
示例如下:
let arr = [{id:1,name:'小明'},{id:1,name:'小南'},{id:3,name:'小明'}]
根据对象属性name去重
duplicateRemovalArr (arr,'name')
执行结果如下:
执行结果.png