我有2个嵌套数组,我想检查list1
中是否有id,list2
中是否有相同的id,请添加list2
+ {{1} }和tag
从count
到新数组。新数组包含list1
,tag
和count
中id的详细信息列表与list1
注意:这2个列表的大小不同
预先感谢您的帮助
例如:
列表1
list2
列表2
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
}
}
]
新数组
[
{
"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",
}
]
答案 0 :(得分: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
中的 all 个项目,而无论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);
答案 1 :(得分:1)
您可以使用array.forEach,
首先,您需要获取list1并遍历每个项目,在每个项目内您都有名为id的属性,该属性再次是数组的集合。您需要使用id进行一次foreach并检查list2,一旦list2.id与list 1的ID匹配。它应该推送您所需输出的对象。
我希望下面的代码能解决这个问题。
{{1}}
答案 2 :(得分:0)
非常感谢您的回复, 那天我可以通过以下方式解决它:
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);
}
再次感谢