遍历对象数组并根据多种条件对其进行过滤

时间:2020-06-17 15:52:20

标签: javascript arrays object

我有一个对象数组,其中包含很多重复/无用的数据。我需要根据客户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);
  }

我需要对这个话题进行更多的研究,但是如果有人可以同时提供帮助,那就太好了。

致谢

2 个答案:

答案 0 :(得分:1)

.popuptext.show {
  display: inline-block; // show element when you have the class name 'show'
}
.popuptext {
  display: none; // hide by default
}

答案 1 :(得分:0)

此代码过滤器重复ID

$keyboard = new Keyboard(...$titles);