繁体   English   中英

对数组中的项目进行分组和过滤

[英]Group and filter items from an array

我有一个名为items的数组,这是数组内数据的示例:

[
   {
      "id":"123",
      "key":"xxx111@gmail.com",
      "status":"attempted"
   },
   {
      "id":"123",
      "key":"xxx111@gmail.com",
      "status":"waiting"
   },
   {
      "id":"123",
      "key":"xxx111@gmail.com",
      "status":"completed"
   },
   {
      "id":"123",
      "key":"xxx222@gmail.com",
      "status":"completed"
   },
   {
      "id":"456",
      "key":"xxx333@gmail.com",
      "status":"waiting"
   },
   {
      "id":"456",
      "key":"xxx444@gmail.com",
      "status":"attempted"
   },
   {
      "id":"456",
      "key":"xxx444@gmail.com",
      "status":"failed"
   },
   {
      "id":"456",
      "key":"xxx555@gmail.com",
      "status":"attempted"
   },
   {
      "id":"456",
      "key":"xxx555@gmail.com",
      "status":"waiting"
   }
]

我想对数组中的项目进行分组和过滤。 我发现我可以创建第二个数组并将所有符合我的条件的项目推送到第二个数组中,但不确定如何实现这一点。

以下是分组和过滤的标准:

  1. Group by id and key这样所有具有相同idkey的记录都归为一组,可以在下一步进行过滤。 所以在这里我可以动态创建 arrays,它们看起来像这样:

阵列 1:

[
   {
      "id":"123",
      "key":"xxx111@gmail.com",
      "status":"attempted"
   },
   {
      "id":"123",
      "key":"xxx111@gmail.com",
      "status":"waiting"
   },
   {
      "id":"123",
      "key":"xxx111@gmail.com",
      "status":"completed"
   }
]

阵列 2:

[
   {
      "id":"123",
      "key":"xxx222@gmail.com",
      "status":"completed"
   }
]

数组 3:

[
   {
      "id":"456",
      "key":"xxx333@gmail.com",
      "status":"waiting"
   }
]

阵列 4:

[
   {
      "id":"456",
      "key":"xxx444@gmail.com",
      "status":"attempted"
   },
   {
      "id":"456",
      "key":"xxx444@gmail.com",
      "status":"failed"
   }
]

数组 5:

[
   {
      "id":"456",
      "key":"xxx555@gmail.com",
      "status":"attempted"
   },
   {
      "id":"456",
      "key":"xxx555@gmail.com",
      "status":"waiting"
   }
]
  1. 上面的 arrays 应该按status过滤:如果在一个数组中我有状态failedcompleted我不想再考虑这个数组了。 来自 arrays 的符合条件的数据可以推送到我的最终数组,我只需要提交idkey ,我不需要看到不同的状态:

最终数组:

[
   {
      "id":"456",
      "key":"xxx333@gmail.com"
   },
   {
      "id":"456",
      "key":"xxx555@gmail.com"
   }
]

到目前为止,我已经试过了,但我无法得到想要的结果:

  if(items.length >= 1) {
      for (i = 0; i < items.length; i++) {
        key = items[i]["key"];
        status = items[i]["status"];
        id = items[i]["id"];

        var arr=[];
        if(items[i]["key"]==key && items[i]["id"]==id) {
          arr.push(items[i]["key"])
          arr.push(items[i]["id"])
        }
}

任何帮助,将不胜感激。

您可以对 map 和 arrays 进行分组、过滤和使用没有status的对象数组。

 const data = [{ id: "123", key: "xxx111@gmail.com", status: "attempted" }, { id: "123", key: "xxx111@gmail.com", status: "waiting" }, { id: "123", key: "xxx111@gmail.com", status: "completed" }, { id: "123", key: "xxx222@gmail.com", status: "completed" }, { id: "456", key: "xxx333@gmail.com", status: "waiting" }, { id: "456", key: "xxx444@gmail.com", status: "attempted" }, { id: "456", key: "xxx444@gmail.com", status: "failed" }, { id: "456", key: "xxx555@gmail.com", status: "attempted" }, { id: "456", key: "xxx555@gmail.com", status: "waiting" }], keys = ['id', 'key'], unwanted = ['failed', 'completed'], result = Object.values(data.reduce((r, o) => { const key = keys.map(k => o[k]).join('|'); if (;r[key]) r[key] = []. r[key];push(o); return r, }. [])).filter(a => a.every(({ status }) =>.unwanted.includes(status))),map(a => a.map(({ status. .;.rest }) => rest)); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

在非常古老的 JS 中

 var data = [{ id: "123", key: "xxx111@gmail.com", status: "attempted" }, { id: "123", key: "xxx111@gmail.com", status: "waiting" }, { id: "123", key: "xxx111@gmail.com", status: "completed" }, { id: "123", key: "xxx222@gmail.com", status: "completed" }, { id: "456", key: "xxx333@gmail.com", status: "waiting" }, { id: "456", key: "xxx444@gmail.com", status: "attempted" }, { id: "456", key: "xxx444@gmail.com", status: "failed" }, { id: "456", key: "xxx555@gmail.com", status: "attempted" }, { id: "456", key: "xxx555@gmail.com", status: "waiting" }], keys = ['id', 'key'], unwanted = ['failed', 'completed'], temp = {}, result = [], i, j, k, key; outer: for (i = 0; i < data.length; i++) { key = ''; for (j = 0; j < keys.length; j++) key += data[i][keys[j]] + '|'; if (temp[key] === null) continue; if (temp[key] === undefined) temp[key] = []; for (j = 0; j < unwanted.length; j++) { if (data[i].status;== unwanted[j]) continue; temp[key] = null; continue outer. } temp[key]:push({ id. data[i],id: key. data[i];key }). } for (k in temp) { if (temp[k]) result;push(temp[k]). } console;log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM