首页 > 解决方案 > 检查嵌套数组是否包含 JavaScript 中另一个嵌套数组的任何元素

问题描述

我有2个嵌套数组,我想检查idlist1是否从和中的id 相同list2list2tagcountlist1tagcountlist1list2

注意:这两个列表的大小不同

提前感谢您的帮助

例如:

列表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",
    }
 ]
}
]

标签: javascriptarrayssearch

解决方案


遍历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);


推荐阅读