对数组中的项目进行分组和过滤
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"
}
]
我想对数组中的项目进行分组和过滤。我发现我可以创建第二个数组并将所有符合我的条件的项目推送到第二个数组中,但不确定如何实现。
分组过滤的条件如下:
- Group by
id
和 key
这样所有具有相同 id
和 key
的记录都被分组在一起,可以在下一步中进行过滤。所以在这里我可以动态创建数组,它们看起来像这样:
数组 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"
}
]
- 以上数组应按
status
过滤:如果在数组中我有状态 failed
或 completed
我不想再考虑这个数组了。符合条件的数组中的数据可以推送到我的最终数组,我只需要提交 id
和 key
,我不需要查看不同的状态:
最终数组:
[
{
"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"])
}
}
如有任何帮助,我们将不胜感激。
您可以在没有 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; }
我有一个名为 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"
}
]
我想对数组中的项目进行分组和过滤。我发现我可以创建第二个数组并将所有符合我的条件的项目推送到第二个数组中,但不确定如何实现。
分组过滤的条件如下:
- Group by
id
和key
这样所有具有相同id
和key
的记录都被分组在一起,可以在下一步中进行过滤。所以在这里我可以动态创建数组,它们看起来像这样:
数组 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"
}
]
- 以上数组应按
status
过滤:如果在数组中我有状态failed
或completed
我不想再考虑这个数组了。符合条件的数组中的数据可以推送到我的最终数组,我只需要提交id
和key
,我不需要查看不同的状态:
最终数组:
[
{
"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"])
}
}
如有任何帮助,我们将不胜感激。
您可以在没有 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; }