我有一個包含大量重復/無用數據的對象數組。我需要根據客戶 id 進行過濾并選擇具有最新日期的對象。數據看起來像這樣:let data = [ { CUSTOMER_PERMANENT_ID: "2495", EMAIL: "[email protected]", EVENT_ACTIVATION_TIME: "2019-10-25 13:57:38.79", }, { CUSTOMER_PERMANENT_ID: "2495", EMAIL: "[email protected]", EVENT_ACTIVATION_TIME: "2019-10-28 20:04:49.016", }, { CUSTOMER_PERMANENT_ID: "2495", EMAIL: "[email protected]", EVENT_ACTIVATION_TIME: "2019-10-28 20:04:49.019", }, { CUSTOMER_PERMANENT_ID: "5995", EMAIL: "[email protected]", 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); }我需要更多地研究這個話題,但是如果有人能在此期間提供幫助,那就太好了。
遍歷對象數組并根據多個條件對其進行過濾
幕布斯6054654
2022-10-08 15:18:28