我有一个包含大量重复/无用数据的对象数组。我需要根据客户 id 进行过滤并选择具有最新日期的对象。数据看起来像这样:
let data = [
{
CUSTOMER_PERMANENT_ID: "2495",
EMAIL: "abs@gmail.com",
EVENT_ACTIVATION_TIME: "2019-10-25 13:57:38.79",
},
{
CUSTOMER_PERMANENT_ID: "2495",
EMAIL: "abs@gmail.com",
EVENT_ACTIVATION_TIME: "2019-10-28 20:04:49.016",
},
{
CUSTOMER_PERMANENT_ID: "2495",
EMAIL: "abs@gmail.com",
EVENT_ACTIVATION_TIME: "2019-10-28 20:04:49.019",
},
{
CUSTOMER_PERMANENT_ID: "5995",
EMAIL: "John@gmail.com",
EVENT_ACTIVATION_TIME: "2019-10-28 17:24:10.98",
}
]
我尝试了以下功能,但它仅在有两个重复对象时才有效,如果有两个以上,则返回所有对象。
public fixDcppSelectedClientData() {
let result = [];
for (let item of this.arr) {
for (let checkingItem of this.arr) {
if (
this.arr.indexOf(item) !=
this.arr.indexOf(checkingItem) &&
item.CUSTOMER_PERMANENT_ID == checkingItem.CUSTOMER_PERMANENT_ID &&
new Date(item.EVENT_ACTIVATION_TIME).getTime() <
new Date(checkingItem.EVENT_ACTIVATION_TIME).getTime()
) {
if (result.indexOf(checkingItem) == -1) {
result.push(checkingItem);
}
}
}
}
console.log("filtered data is ", result);
}
我需要更多地研究这个话题,但是如果有人能在此期间提供帮助,那就太好了。
ibeautiful
忽然笑
相关分类