javascript - 检查嵌套数组是否包含 JavaScript 中另一个嵌套数组的任何元素
问题描述
我有2个嵌套数组,我想检查idlist1
是否从和中的id 相同list2
list2
tag
count
list1
tag
count
list1
list2
注意:这两个列表的大小不同
提前感谢您的帮助
例如:
列表1
const list1 = [
{
"id": [
"5cca1dbc-dd5c-498f-8f83-735062c05240",
"2a10c30a-7c3a-4081-8246-9d37e19c2d6f",
"3128f36c-1c79-4301-b08f-e0182c256c03"
],
"tag": "tag1",
"count": {
"low": 53,
"high": 0
}
},
{
"id": [
"510019af-1728-4628-9019-343cd3c1b3e1",
"fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"adf0cd4c-3072-4ecf-9aa7-ecd5580c31ae"
],
"tag": "tag2",
"count": {
"low": 43,
"high": 0
}
}
]
清单2
[
{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "efde2bc9-018b-49c1-9c01-a4eda9817a33",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
]
新阵列
[
{
"tag": "tag1",
"count": {
"low": 53,
"high": 0
},
"details": [
{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [
{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}
],
"topics": [
{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
]
}
]
解决方案
遍历list1
,检查是否id
存在list2
以及是否将其添加到新数组中。
例如
var result = [];
for (let item of list1) {
let details = list2.filter(l2 => item.id.includes(l2.id));
if (details.length > 0) {
result.push({
tag: item.tag,
count: item.count,
details: details
});
}
}
如果您希望in 中的所有项目list1
都显示出来,而不管in 中是否id
存在,list2
您可以使用map
并为中的每个项目返回一个新对象list1
。
var result = list1.map(l1 => {
return {
tag: l1.tag,
count: l1.count,
details: list2.filter(l2 => l1.id.includes(l2.id))
};
});
const list1 = [{
"id": [
"5cca1dbc-dd5c-498f-8f83-735062c05240",
"2a10c30a-7c3a-4081-8246-9d37e19c2d6f",
"3128f36c-1c79-4301-b08f-e0182c256c03"
],
"tag": "tag1",
"count": {
"low": 53,
"high": 0
}
},
{
"id": [
"510019af-1728-4628-9019-343cd3c1b3e1",
"fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"adf0cd4c-3072-4ecf-9aa7-ecd5580c31ae"
],
"tag": "tag2",
"count": {
"low": 43,
"high": 0
}
}
];
const list2 = [{
"id": "5cca1dbc-dd5c-498f-8f83-735062c05240",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}],
"topics": [{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "fb420746-4d11-4d2e-ab7f-b8a73e5b8f8e",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}],
"topics": [{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
},
{
"id": "efde2bc9-018b-49c1-9c01-a4eda9817a33",
"createdDate": "2017-10-08T22:40:33.020Z",
"modifiedDate": "2017-10-08T22:40:33.020Z",
"title": "Good morning! #tag1",
"text": " ",
"media": [{
"id": "1f8c564c-91f1-457c-b4c1-0820c03861b4",
"metadata": {
"mimetype": "image/jpeg",
"imageHeight": 400,
"imageWidth": 300
}
}],
"topics": [{
"topicId": "22a96a83-def3-4981-bc91-9277464b7105"
},
{
"name": "Fashion",
"topicId": "6d4caea2-8387-42f3-977d-06a4bb063c44"
}
],
"language": null,
"sourceId": "d25205ca-2ef308261113",
}
];
var result1 = [];
for (let item of list1) {
let details = list2.filter(l2 => item.id.includes(l2.id));
if (details.length > 0) {
result1.push({
tag: item.tag,
count: item.count,
details: details
});
}
}
console.log(result1);
var result2 = list1.map(l1 => {
return {
tag: l1.tag,
count: l1.count,
details: list2.filter(l2 => l1.id.includes(l2.id))
};
});
console.log(result2);
推荐阅读
- java - 活动不适用于添加导航抽屉
- xamarin - 单击“单元格控件”之类的按钮时获取ListView项目
- web-scraping - 如何在使用 Javascript 或 Json 的 Reddit 中抓取具有相同类名的项目?
- vue.js - Vue.js:字段值一旦失去焦点就会返回
- python - 如何计算重复的数组值并返回它们的索引位置?
- python - 如何在 Unix 平台上为机器人框架 API python 测试创建 jenkins 作业
- vbscript - QTP/UFT 在函数中重用对象变量
- amazon-web-services - 如何使用 aws-cli 命令重定向 AWS 负载均衡器的流量?
- python - 为什么使用 f-strings 或字符串格式而不是 + 进行连接?
- html - 如何创建带有渐变的曲线?