数组中对象去重

时间:2023-02-10 23:17:39
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)
});

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