js数组去重用reduce

文章展示了如何使用JavaScript的Array.prototype.reduce方法,通过创建一个对象来跟踪已出现的device_id,从而实现数组中的重复项去除,特别是针对具有相同device_id的对象。最终输出为去重后的设备数据列表。
摘要由CSDN通过智能技术生成
  1. 格式如下
  2. resData=[{device_id: 290, handled_by: '', org: '1号分公司',  description: '' "}, {device_id: 290, handled_by: '', org: '1号分公司',  description: '' "}, {device_id: 230, handled_by: '', org: '2号分公司',  description: '' "}, {device_id: 240, handled_by: '', org: '3号分公司',  description: '' "}, {device_id: 250, handled_by: '', org: '4号分公司',  description: '' "}, {device_id: 290, handled_by: '', org: '1号分公司',  description: '' "}, {device_id: 290, handled_by: '', org: '1号分公司',  description: '' "}]

去重处理

                let resobj={};

                resData = resData.reduce((item,next) => {

                    resobj[next.device_id] ? '' : resobj[next.device_id] = true && item.push(next);

                    return item;

                },[]);

console.log(resData)

控制台打印:

[{device_id: 290, handled_by: '', org: '1号分公司',  description: '' "}, {device_id: 230, handled_by: '', org: '2号分公司',  description: '' "}, {device_id: 240, handled_by: '', org: '3号分公司',  description: '' "}, {device_id: 250, handled_by: '', org: '4号分公司',  description: '' "},]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值