我有一个像这样的JSON对象摘要
[
{
"Project": "P1234",
"Domain": "Domain1",
"Manager": "Manager1",
"Milestone":[],
"Artefacts":[]
},
{
"Project": "P2345",
"Domain": "Domain2",
"Manager": "Manager2",
"Milestone":[],
"Artefacts":[]
},
{
"Project": "P3277",
"Domain": "Domain3",
"Manager": "Manager1",
"Milestone":[],
"Artefacts":[]
}
]
和这样一个详细的集合
{
"Artefacts": [
{
"Project": "P1234",
"ArtefactType": "Art1",
"Date": "15/10/2021"
},
{
"Project": "P1234",
"ArtefactType": "Art2",
"Date": "10/11/2020"
},
{
"Project": "P1234",
"ArtefactType": "Art3",
"Date": "5/10/2021"
},
{
"Project": "P2345",
"ArtefactType": "Art1",
"Date": "15/10/2021"
},
{
"Project": "P2345",
"ArtefactType": "Art7",
"Date": "06/05/2021"
}
],
"Milestones": [
{
"Project": "P1234",
"Type": "M1",
"MilestoneDetails": "Details"
},
{
"Project": "P1234",
"Type": "M2",
"MilestoneDetails": "Details"
},
{
"Project": "P1234",
"Type": "M3",
"MilestoneDetails": "Details"
},
{
"Project": "P2345",
"Type": "M1",
"MilestoneDetails": "Details"
},
{
"Project": "P2345",
"Type": "M2",
"MilestoneDetails": "Details"
},
{
"Project": "P2345",
"Type": "M3",
"MilestoneDetails": "Details"
},
{
"Project": "P3277",
"Type": "M1",
"MilestoneDetails": "Details"
},
{
"Project": "P3277",
"Type": "M2",
"MilestoneDetails": "Details"
},
{
"Project": "P3277",
"Type": "M3",
"MilestoneDetails": "Details"
}
]
}
我在2个API请求中从服务器获得摘要和细节。因此,我需要使用Project Code从详细表中映射汇总表的里程碑和工件。
this.clSummary.forEach(element=>{
element.Milestone=this.getMilestones(element.ProjectCode);
element.Artefacts=this.getArtefacts(element.ProjectCode);
})
使用任何箭头函数或linq是一种好方法还是一种简单或先进的方法?
我假设你正在寻找这样的东西。
summary.forEach((sum) => {
sum.Artefacts = details.Artefacts.filter(
({ Project }) => Project === sum.Project
);
sum.Milestone = details.Milestones.filter(
({ Project }) => Project === sum.Project
);
});
返回更新后的摘要如下所示。
[
{
Project: "P1234",
Domain: "Domain1",
Manager: "Manager1",
Artefacts: [
{
ArtefactType: "Art1",
Date: "15/10/2021",
Project: "P1234",
},
{
ArtefactType: "Art2",
Date: "10/11/2020",
Project: "P1234",
},
{
ArtefactType: "Art3",
Date: "5/10/2021",
Project: "P1234",
},
],
Milestone: [
{
MilestoneDetails: "Details",
Project: "P1234",
Type: "M1",
},
{
MilestoneDetails: "Details",
Project: "P1234",
Type: "M2",
},
{
MilestoneDetails: "Details",
Project: "P1234",
Type: "M3",
},
],
},
{
Project: "P2345",
Domain: "Domain2",
Manager: "Manager2",
Artefacts: [
{
ArtefactType: "Art1",
Date: "15/10/2021",
Project: "P2345",
},
{
ArtefactType: "Art7",
Date: "06/05/2021",
Project: "P2345",
},
],
Milestone: [
{
MilestoneDetails: "Details",
Project: "P2345",
Type: "M1",
},
{
MilestoneDetails: "Details",
Project: "P2345",
Type: "M2",
},
{
MilestoneDetails: "Details",
Project: "P2345",
Type: "M3",
},
],
},
{
Project: "P3277",
Artefacts: [],
Domain: "Domain3",
Manager: "Manager1",
Milestone: [
{
MilestoneDetails: "Details",
Project: "P3277",
Type: "M1",
},
{
MilestoneDetails: "Details",
Project: "P3277",
Type: "M2",
},
{
MilestoneDetails: "Details",
Project: "P3277",
Type: "M3",
},
],
},
]