let hash = {};
let config = [{
name: 2,
state: true,
output: 'Y',
}, {
name: 3,
state: true,
output: 'A',
}, {
name: 5,
state: true,
output: 'S',
}, {
name: 7,
state: true,
output: 'B',
}];
config = [...config, {
name: 3,
state: false,
output: 'A',
}]
const newArr = config.reduceRight((item, next) => {
hash[next.name] ? '' : hash[next.name] = true && item.push(next);
return item
}, []);
//根据数组对象唯一标识去重 提示:首先hash空对象要有的
function arrayObject_heavy(arr) {
let hash = {};
const newArr = arr.reduceRight((item, next) => {
hash[next.name] ? '' : hash[next.name] = true && item.push(next);
return item
}, []);
//封装成方法,需要传一个数组参数,并更改唯一标识符即可
return newArr;
}
原作者链接:https://www.jianshu.com/p/41e42a7b2d0f