我有以下数组:
const arr1 = [
{"id": 699306, "address": "On earth" },
{"id": 701298, "address": "On earth" },
{"id": 701299, "address": "On earth" },
{"id": 701300, "address": "On earth" }
];
另一个数组非常复杂,如下所示:
const arr2 = [
{
"included": [
{
"id": 699306,
"levelColumn": "CLAIM_DATE",
"levelColumnName": "Claims",
"isIncluded": true,
"tabs": [
{
"tabId": 1231,
"tabName": "Files"
}
],
"levels": [
{
"permValue": "01/01/2021",
"permName": "01/01/2021"
},
{
"permValue": "01/01/2022",
"permName": "01/01/2022"
},
{
"permValue": "01/01/2023",
"permName": "01/01/2023"
},
{
"permValue": "01/01/2024",
"permName": "01/01/2024"
},
{
"permValue": "01/01/2025",
"permName": "01/01/2025"
},
{
"permValue": "01/02/2023",
"permName": "01/02/2023"
},
{
"permValue": "01/02/2024",
"permName": "01/02/2024"
},
{
"permValue": "01/03/2024",
"permName": "01/03/2024"
},
{
"permValue": "01/03/2026",
"permName": "01/03/2026"
},
{
"permValue": "01/04/2022",
"permName": "01/04/2022"
},
{
"permValue": "01/04/2023",
"permName": "01/04/2023"
},
{
"permValue": "01/04/2024",
"permName": "01/04/2024"
}
]
},
{
"id": 701298,
"levelColumn": "CLAIM_DATE",
"levelColumnName": "Claims",
"isIncluded": true,
"tabs": [
{
"tabId": 1231,
"tabName": "Files"
}
],
"levels": [
{
"permValue": "01/01/2021",
"permName": "01/01/2021"
},
{
"permValue": "01/01/2022",
"permName": "01/01/2022"
}
]
},
{
"id": 647298,
"levelColumn": "SOURCE_FILE_ID",
"levelColumnName": "Files",
"isIncluded": true,
"tabs": [
{
"tabId": 1231,
"tabName": "Files"
},
{
"tabId": 1251,
"tabName": "Files - noinv"
}
],
"levels": [
{
"permValue": "2028524",
"permName": "0000841354-LAMB-0303043272-11_17_2023_A_Gold.txt"
},
{
"permValue": "2092815",
"permName": "0000841354-LAMB-0305232305-051324_A_Gold.txt"
},
{
"permValue": "2093469",
"permName": "0000841354-LAMB-1135418182-051624_A_Gold.txt"
},
{
"permValue": "2125382",
"permName": "0000841354-LAMB-303368862-12_12_23_3020_Gold.txt"
}
]
},
{
"id": 699307,
"levelColumn": "CLAIM_DATE",
"levelColumnName": "Claims",
"isIncluded": true,
"tabs": [
{
"tabId": 1231,
"tabName": "Files"
}
],
"levels": [
{
"permValue": "01/01/2021",
"permName": "01/01/2021"
},
{
"permValue": "01/01/2022",
"permName": "01/01/2022"
},
{
"permValue": "01/01/2023",
"permName": "01/01/2023"
},
{
"permValue": "01/01/2024",
"permName": "01/01/2024"
},
{
"permValue": "01/01/2025",
"permName": "01/01/2025"
},
{
"permValue": "01/02/2023",
"permName": "01/02/2023"
},
{
"permValue": "01/02/2024",
"permName": "01/02/2024"
},
{
"permValue": "01/03/2024",
"permName": "01/03/2024"
},
{
"permValue": "01/03/2026",
"permName": "01/03/2026"
},
{
"permValue": "01/04/2022",
"permName": "01/04/2022"
},
{
"permValue": "01/04/2023",
"permName": "01/04/2023"
},
{
"permValue": "01/04/2024",
"permName": "01/04/2024"
}
]
},
{
"id": 701298,
"levelColumn": "MSTR_ENTERPRISE_ID",
"levelColumnName": "Enterprise",
"isIncluded": true,
"tabs": [
{
"tabId": 1971,
"tabName": "Users"
}
],
"levels": [
{
"permValue": "23",
"permName": "Aramark"
},
{
"permValue": "693",
"permName": "Aryzta"
}
]
},
{
"id": 693298,
"levelColumn": "E1_APP_USER_ID",
"levelColumnName": "User",
"isIncluded": true,
"tabs": [
{
"tabId": 1971,
"tabName": "Users"
}
],
"levels": [
{
"permValue": "137064",
"permName": " (tibersoftsmuckers [137064])"
},
{
"permValue": "136184",
"permName": " QA (tsoceanspray [136184])"
}
]
},
{
"id": 693298,
"levelColumn": "E1_APP_USER_TYPE",
"levelColumnName": "User Type",
"isIncluded": true,
"tabs": [
{
"tabId": 1971,
"tabName": "Users"
}
],
"levels": [
{
"permValue": "Client",
"permName": "Client"
},
{
"permValue": "Not Provided",
"permName": "Not Provided"
},
{
"permValue": "Tibersoft",
"permName": "Tibersoft"
}
]
}
],
"excluded": [
{
"id": 368302,
"levelColumn": "E1_APP_USER_ID",
"levelColumnName": "User",
"isIncluded": false,
"tabs": [
{
"tabId": 1971,
"tabName": "Users"
}
],
"levels": [
{
"permValue": "575301",
"permName": " Kelley Mahowald (kmahowald [575301])"
},
{
"permValue": "134560",
"permName": " Support (support [134560])"
},
{
"permValue": "135241",
"permName": " Support (support [135241])"
},
{
"permValue": "138248",
"permName": " Support (support [138248])"
},
{
"permValue": "136725",
"permName": " Tibersoftaramark (tibersoftaramark [136725])"
},
{
"permValue": "137705",
"permName": " Tibersoftjmfg (tibersoftjmfg [137705])"
},
{
"permValue": "138508",
"permName": " Tibersoftlw (tibersoftlw [138508])"
},
{
"permValue": "139330",
"permName": " Tibersoftschwans (tibersoftschwans [139330])"
},
{
"permValue": "136726",
"permName": " Tsconagra (tsaramark [136726])"
},
{
"permValue": "134466",
"permName": "(abailey [134466])"
},
{
"permValue": "134284",
"permName": "(afitzgerald [134284])"
},
{
"permValue": "137484",
"permName": "(c [137484])"
},
{
"permValue": "31346",
"permName": "(caradp [31346])"
},
{
"permValue": "31658",
"permName": "(caramarie [31658])"
}
]
}
]
}
]
所以我想要的是将两个数组合并为一个,如下所示:
const arr1 = [
{"id": 699306, "address": "On earth", "included": [items from arr2], "excluded": [items from arr2] },
{"id": 701298, "address": "On earth", "included": [items from arr2], "excluded": [items from arr2] },
{"id": 701299, "address": "On earth", "included": [], "excluded": [] }, //As there is no matching for these ids
{"id": 701300, "address": "On earth", "included": [], "excluded": [] }
];
我知道,这种关系看起来不合适,但这就是我现在的情况。有什么办法可以映射它们吗?
我尝试了以下操作,但它对于包含和排除对象都返回 null:
mapArraysById(array1: Item[], array2: Item[]): Item[] {
const mergedArray: Item[] = [];
for (let i = 0; i < array1.length; i++) {
const item1 = array1[i];
console.log("id: " + item1.id);
const item2 = array2.find(item => item.id === item1.id);
if (item2) {
console.log("Item 2:");
console.log(item1 + " " + item2);
mergedArray.push({ ...item1, ...item2 });
} else {
mergedArray.push({ ...item1, included: [], excluded: [] });
}
}
return mergedArray;
}
尝试迭代
arr1
并获取 arr2
中与 id 匹配的包含和排除项目。最后,将包含和排除的项目返回到结果,并且它们都不匹配返回空数组。
参考以下代码参考:
const arr1 = [
{ id: 699306, address: "On earth" },
{ id: 701298, address: "On earth" },
{ id: 701299, address: "On earth" },
{ id: 701300, address: "On earth" },
];
const arr2 = [
{
included: [
{
id: 699306,
levelColumn: "CLAIM_DATE",
levelColumnName: "Claims",
isIncluded: true,
tabs: [{ tabId: 1231, tabName: "Files" }],
levels: [
{ permValue: "01/01/2021", permName: "01/01/2021" },
{ permValue: "01/01/2022", permName: "01/01/2022" },
],
},
{
id: 701298,
levelColumn: "CLAIM_DATE",
levelColumnName: "Claims",
isIncluded: true,
tabs: [{ tabId: 1231, tabName: "Files" }],
levels: [
{ permValue: "01/01/2021", permName: "01/01/2021" },
{ permValue: "01/01/2022", permName: "01/01/2022" },
],
},
],
excluded: [
{
id: 368302,
levelColumn: "E1_APP_USER_ID",
levelColumnName: "User",
isIncluded: false,
tabs: [{ tabId: 1971, tabName: "Users" }],
levels: [
{ permValue: "575301", permName: " Kelley Mahowald (kmahowald [575301])" },
],
},
],
},
];
const mergeArrays = (arr1, arr2) => {
const { included, excluded } = arr2[0]; // get included and excluded from arr2
return arr1.map(item => {
const includedItems = included.filter(i => i.id === item.id); // get matching included items
const excludedItems = excluded.filter(e => e.id === item.id); // get matching excluded items
return {
...item,
included: includedItems.length > 0 ? includedItems : [],
excluded: excludedItems.length > 0 ? excludedItems : [],
};
});
};
console.log(JSON.stringify(mergeArrays(arr1, arr2)));