0
点赞
收藏
分享

微信扫一扫

js 数组对象去重方法,根据唯一标识进行去重

WikongGuan 2022-03-12 阅读 161
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

举报

相关推荐

0 条评论