I have project details , which I am getting from two calles, first call contains few fields and another one contains few fields of project details. I have to merge.
projectDetails1
[
{
ProjectID: 284,
ProjectName: "Test Project June-30",
Budget: 10000,
BudgetMoney: "10,000.00",
BriefStatus: "Approved",
ProjectStatus: "Estimating",
AID: "B1124-022571-00",
},
{
ProjectID: 285,
ProjectName: "Test Project June-28",
Budget: 10000,
BudgetMoney: "10,000.00",
BriefStatus: "Approved",
ProjectStatus: "Estimating",
AID: "B1124-022571-11",
},
{
ProjectID: 286,
ProjectName: "Test Project June-28",
Budget: 10000,
BudgetMoney: "10,000.00",
BriefStatus: "Approved",
ProjectStatus: "Estimating",
AID: "B1124-022571-22",
}
]
projectDetails2
[
{
a_ID: "B1124-022571-00",
Start_Date: "2022-05-09T00:00:00.000Z",
End_Date: "2023-05-02T00:00:00.000Z",
Project_Type_ID: 12,
Project_Type: "Ext. Delivery (T&M)",
Active_YN: "Y",
Committed_Revenue: 3000000,
Revenue: 4000000,
},
{
a_ID: "B1124-022571-11",
Start_Date: "2022-04-04T00:00:00.000Z",
End_Date: "2022-12-30T00:00:00.000Z",
Project_Type_ID: 12,
Project_Type: "Ext. Delivery (T&M)",
Active_YN: "Y",
Committed_Revenue: 3000000,
Revenue: 4000000,
},
]
Need to merge both and by aID and a_ID is common(joining) fields.
finalProjectDeatils:
[
{
ProjectID: 284,
ProjectName: "Test Project June-30",
Budget: 10000,
BudgetMoney: "10,000.00",
BriefStatus: "Approved",
ProjectStatus: "Estimating",
AID: "B1124-022571-00",
Start_Date: "2022-05-09T00:00:00.000Z",
End_Date: "2023-05-02T00:00:00.000Z",
Project_Type_ID: 12,
Project_Type: "Ext. Delivery (T&M)",
Active_YN: "Y",
Committed_Revenue: 3000000,
Revenue: 4000000,
},
{
ProjectID: 285,
ProjectName: "Test Project June-28",
Budget: 10000,
BudgetMoney: "10,000.00",
BriefStatus: "Approved",
ProjectStatus: "Estimating",
AID: "B1124-022571-11",
Start_Date: "2022-04-04T00:00:00.000Z",
End_Date: "2022-12-30T00:00:00.000Z",
Project_Type_ID: 12,
Project_Type: "Ext. Delivery (T&M)",
Active_YN: "Y",
Committed_Revenue: 3000000,
Revenue: 4000000,
},
{
ProjectID: 286,
ProjectName: "Test Project June-28",
Budget: 10000,
BudgetMoney: "10,000.00",
BriefStatus: "Approved",
ProjectStatus: "Estimating",
AID: "B1124-022571-22",
}
]
3
Answers
Make a a lookup object out of
projectDetails2
:Then merge each element from
projectDetails1
with a corresponding element from the lookup object, if possible:You could use reduce to merge each item under its AID or a_ID key, then take the values of the resulting object.
Concatenate the two arrays.
Run reduce on the combined array.
Within the reducer function, extract AID from the item, and use a_ID as a fallback. This captures the id in AID for either case and has the side effect of removing
a_ID
from the results. Capture the remaining attributes with a rest operator (...other
).Merge the attributes into the accumulator object under the ID key. If the key has already been encountered this will merge the current item’s attributes into the prior entry. Return the updated accumulator for the next iteration.
Finally, extract the merged values via Object.values.