数组中对象去重

本文介绍了一种使用JavaScript去除数组中重复对象的方法。通过将对象转换为字符串并利用JSON的特性来实现唯一性的判断,最终得到不含重复项的优惠券数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

let coupons=[ { "id": 95, "appId": "mitang", "deleted": false, "creator": null, "created": 1520560522000, "modifier": null, "modified": 1520560522000, "pageSize": null, "offset": null, "type": 1, "typeId": 76, "userId": 108, "guideId": 32216, "storeId": null, "couponId": 3, "couponSn": "mt0000000001", "startTime": 1520560522000, "endTime": 1520560522000, "couponName": "发生的发生", "remark": "", "couponPackId": null, "writeOff": 0, "face": "10" }, { "id": 95, "appId": "mitang", "deleted": false, "creator": null, "created": 1520560522000, "modifier": null, "modified": 1520560522000, "pageSize": null, "offset": null, "type": 1, "typeId": 76, "userId": 108, "guideId": 32216, "storeId": null, "couponId": 3, "couponSn": "mt0000000001", "startTime": 1520560522000, "endTime": 1520560522000, "couponName": "发生的发生", "remark": "", "couponPackId": null, "writeOff": 0, "face": "10" } ]
let unique = {};
coupons.forEach(function (gpa) {
unique[JSON.stringify(gpa)] = gpa
});
coupons = Object.keys(unique).map(function (u) {
return JSON.parse(u)
});

  数组中不支持直接比较对象;

转载于:https://www.cnblogs.com/shuaitong/p/8583088.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值