【问题标题】:Check if a nested array contains any element of another nested array in JavaScript检查嵌套数组是否包含 JavaScript 中另一个嵌套数组的任何元素
【发布时间】:2018-12-12 05:25:24
【问题描述】:

我有 2 个嵌套数组,我想检查 list1 中的 id 以及 list2 中是否有相同的 id,添加 list2 + tagcount 的对象987654326@ 到一个新数组。新数组有tag,countlist1 中的id 的详细信息列表,与list2 中的id 相同

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

提前感谢您的帮助

例如:

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

【问题讨论】:

  • 你能把 list2 正确发布吗,括号上有一些错误。 List1 很好
  • 我想从list1id 列表中搜索,如果list2 中有任何相同的id,请从list2 中获取这些信息,例如:id @ 987654339@ 在list1 中的列表id 中,所以list2 中也有相同的id,所以我将添加tag,count ,并将此id 的详细信息添加到新列表
  • 你能像list1一样发布list2吗,list2上缺少一些括号。使用给定数据发布解决方案将很有帮助
  • @DILEEPTHOMAS THOMAS 更新
  • 请检查解决方案,让我知道它是否适合您。

标签: javascript arrays search


【解决方案1】:

遍历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 并为@987654330 中的每个项返回一个新对象@。

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

【讨论】:

    【解决方案2】:

    你可以使用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)

    【讨论】:

      【解决方案3】:

      非常感谢您的回复, 那天我可以通过这个解决它:

      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);
        }
      

      再次感谢

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        • 2022-01-02
        相关资源
        最近更新 更多