簡體   English   中英

檢查嵌套數組是否包含JavaScript中另一個嵌套數組的任何元素

[英]Check if a nested array contains any element of another nested array in JavaScript

我有2個嵌套數組,我想檢查list1是否有id,並且list2是否有相同的id,添加list2 + tag對象並從list1 count到新數組。新數組具有tagcount和與list2 id相同的list1的id的詳細信息列表

注意:這2個列表的大小不同

預先感謝您的幫助

例如:

清單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
        }
    }
]

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

新數組

 [
{
    "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
    });
  }
}

如果要顯示list1 所有項目,而不管list2是否存在id ,都可以使用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); 

您可以使用array.forEach

首先,您需要獲取list1並遍​​歷每個項目,在每個項目內您都有名為id的屬性,該屬性也是數組的集合。 您需要使用id進行一次foreach並檢查list2,一旦list2.id與list 1的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 arr = [] list1.forEach(l1I => { l1I.id.forEach(eID => list2.forEach(l2I => { if(l2I.id === eID){ var obj = {} obj["details"] = l2I; obj["tag"] = l1I.tag; obj["count"] = l1I.count; arr.push(obj); } })) }) console.log("output", arr) 

非常感謝您的回復,當天我可以通過以下方式解決:

const responseValueList = {};
  const hashtagsList = [];

  for (const responseRow of list1) {
    const obj = {};
    obj.hashtagName = responseRow.hashtag;
    obj.hashtagCount = responseRow.count;
    const rateableList = []
    for (const responseSectionTwo of list2) {
      if (responseRow.id.includes(responseSectionTwo.id)) {
        rateableList.push(responseSectionTwo);
      }
    }
    obj.hashtagItems = rateableList;
    hashtagsList.push(obj);
  }

再次感謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM