在JavaScript中通过连接字段合并两个对象。

huangapple go评论74阅读模式
英文:

Merge two objects by join field in javascript

问题

我有项目详情,这些详情来自两个调用,第一个调用包含一些字段,另一个调用包含一些项目详情的字段。我需要合并它们。

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",
}, {
  ProjectID: 287,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}]

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,
}]

需要通过 AIDa_ID 这两个共同字段合并它们。

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",
}, {
  ProjectID: 287,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}]
英文:

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",
}, {
  ProjectID: 287,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}]

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",
}, {
  ProjectID: 287,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}]

答案1

得分: 1

你可以使用reduce方法合并每个项目在其AID或a_ID键下,然后获取结果对象的值。

将两个数组连接起来。

const [...d1, ...d2]

在合并的数组上运行reduce方法。

const [...d1, ...d2].reduce( // ....

在reducer函数内部,从项目中提取AID,并使用a_ID作为备用。这会捕获AID中的id,无论哪种情况,还会剔除结果中的a_ID。使用rest操作符(...other)捕获剩余的属性。

.reduce(acc, {a_ID, AID = a_ID, ...other}) => // ...

将属性合并到累加器对象中的ID键下。如果已经遇到了该键,这将合并当前项目的属性到先前条目中。返回更新后的累加器以供下一次迭代使用。

acc[AID] = { AID, ...acc[AID], ...other };
return acc;

最后,通过Object.values提取合并的值。

const merged = Object.values(byId);

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

const d1 = [{
  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",
}, {
  ProjectID: 287,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}];
  
const d2 = [{
  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,
}];

const byId = [...d1, ...d2].reduce((acc, {a_ID, AID = a_ID, ...other}) => {
  acc[AID] = { AID, ...acc[AID], ...other };
  return acc;
}, {});

const merged = Object.values(byId);

console.log(merged);

<!-- 结束代码段 -->

英文:

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.

const [...d1, ...d2]

Run reduce on the combined array.

const [...d1, ...d2].reduce( // ....

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

.reduce(acc, {a_ID, AID = a_ID, ...other}) =&gt; // ...

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.

acc[AID] = { AID, ...acc[AID], ...other };
return acc;

Finally, extract the merged values via Object.values.

const merged = Object.values(byId);

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const d1 = [{
  ProjectID: 284,
  ProjectName: &quot;Test Project June-30&quot;,
  Budget: 10000,
  BudgetMoney: &quot;10,000.00&quot;,
  BriefStatus: &quot;Approved&quot;,
  ProjectStatus: &quot;Estimating&quot;,
  AID: &quot;B1124-022571-00&quot;,
}, {
  ProjectID: 285,
  ProjectName: &quot;Test Project June-28&quot;,
  Budget: 10000,
  BudgetMoney: &quot;10,000.00&quot;,
  BriefStatus: &quot;Approved&quot;,
  ProjectStatus: &quot;Estimating&quot;,
  AID: &quot;B1124-022571-11&quot;,
}, {
  ProjectID: 286,
  ProjectName: &quot;Test Project June-28&quot;,
  Budget: 10000,
  BudgetMoney: &quot;10,000.00&quot;,
  BriefStatus: &quot;Approved&quot;,
  ProjectStatus: &quot;Estimating&quot;,
  AID: &quot;B1124-022571-22&quot;,
}, {
  ProjectID: 287,
  ProjectName: &quot;Test Project June-28&quot;,
  Budget: 10000,
  BudgetMoney: &quot;10,000.00&quot;,
  BriefStatus: &quot;Approved&quot;,
  ProjectStatus: &quot;Estimating&quot;,
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: &quot;Test Project June-28&quot;,
  Budget: 10000,
  BudgetMoney: &quot;10,000.00&quot;,
  BriefStatus: &quot;Approved&quot;,
  ProjectStatus: &quot;Estimating&quot;,
  AID: null,
}];
  
const d2 = [{
  a_ID: &quot;B1124-022571-00&quot;,
  Start_Date: &quot;2022-05-09T00:00:00.000Z&quot;,
  End_Date: &quot;2023-05-02T00:00:00.000Z&quot;,
  Project_Type_ID: 12,
  Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;,
  Active_YN: &quot;Y&quot;,
  Committed_Revenue: 3000000,
  Revenue: 4000000,
}, {
  a_ID: &quot;B1124-022571-11&quot;,
  Start_Date: &quot;2022-04-04T00:00:00.000Z&quot;,
  End_Date: &quot;2022-12-30T00:00:00.000Z&quot;,
  Project_Type_ID: 12,
  Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;,
  Active_YN: &quot;Y&quot;,
  Committed_Revenue: 3000000,
  Revenue: 4000000,
}];

const byId = [...d1, ...d2].reduce((acc, {a_ID, AID = a_ID, ...other}) =&gt; {
  acc[AID] = { AID, ...acc[AID], ...other };
  return acc;
}, {});

const merged = Object.values(byId);

console.log(merged);

<!-- end snippet -->

答案2

得分: 1

由于提供的数据结构,为了不漏掉任何项目数据,只应选择一种采用查找为基础的方法的解决方案,例如通过 Map

至于提供数据的基本要求,首先必须将 projectDetails2 数组映射成一个条目数组(键-值对/元组),其中(查找)键等于/关联到 "Details 2" 项的 a_ID 值。还应立即对这样的项进行解构,从而将该键和项的剩余数据分开,因此 a_ID 不再是任何新创建的 "Details 2" 数据项的一部分。

然后,最后一步就是另一个简单的 map 任务(这次映射 projectDetails1),对于每个 "Details 1" 项,都会创建一个新项,作为(基于 Object.assign)该 "Details 1" 项和查找的 AID 基础的 "Details 2" 项的合并项。如果后者无法查找/引用,那么合并将只与当前处理的项合并到空对象中。

const 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",
}, {
  ProjectID: 287,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}, {
  ProjectID: 288,
  ProjectName: "Test Project June-28",
  Budget: 10000,
  BudgetMoney: "10,000.00",
  BriefStatus: "Approved",
  ProjectStatus: "Estimating",
  AID: null,
}];

const 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,
}];

const aidBasedMergeDataLookup = new Map(
  projectDetails2
    .map(({ a_ID, ...mergeData }) => [a_ID, mergeData])
);

const mergedProjectDetails = projectDetails1
  .map(dataItem => Object.assign(
    {}, dataItem, aidBasedMergeDataLookup.get(dataItem.AID)
  ));

console.log({ mergedProjectDetails });
英文:

Due to the OP's provided data structures and therefore in order to not miss on any project data, one only should choose a solution which follows a lookup-based approach e.g. via Map.

As for the OP's data base, one has to first map the projectDetails2 array into an array of entries (key-value pair/tuple), where the (lookup) key equals/relates a "Details 2"-item's a_ID value. One also would immediately destructure such an item into said key and the item's rest-data, thus a_ID not being anymore part of any now newly created "Details 2" data-item.

The final step then is just another simple map task (this time mapping projectDetails1), where for each "Details 1"-item one does create a new item as merger (based on Object.assign) of said "Details 1"-item and the looked-up AID based "Details 2"-item. In case the latter can not be looked-up/referred the merger happens with just the current processed item into the empty object.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const projectDetails1 = [{
ProjectID: 284,
ProjectName: &quot;Test Project June-30&quot;,
Budget: 10000,
BudgetMoney: &quot;10,000.00&quot;,
BriefStatus: &quot;Approved&quot;,
ProjectStatus: &quot;Estimating&quot;,
AID: &quot;B1124-022571-00&quot;,
}, {
ProjectID: 285,
ProjectName: &quot;Test Project June-28&quot;,
Budget: 10000,
BudgetMoney: &quot;10,000.00&quot;,
BriefStatus: &quot;Approved&quot;,
ProjectStatus: &quot;Estimating&quot;,
AID: &quot;B1124-022571-11&quot;,
}, {
ProjectID: 286,
ProjectName: &quot;Test Project June-28&quot;,
Budget: 10000,
BudgetMoney: &quot;10,000.00&quot;,
BriefStatus: &quot;Approved&quot;,
ProjectStatus: &quot;Estimating&quot;,
AID: &quot;B1124-022571-22&quot;,
}, {
ProjectID: 287,
ProjectName: &quot;Test Project June-28&quot;,
Budget: 10000,
BudgetMoney: &quot;10,000.00&quot;,
BriefStatus: &quot;Approved&quot;,
ProjectStatus: &quot;Estimating&quot;,
AID: null,
}, {
ProjectID: 288,
ProjectName: &quot;Test Project June-28&quot;,
Budget: 10000,
BudgetMoney: &quot;10,000.00&quot;,
BriefStatus: &quot;Approved&quot;,
ProjectStatus: &quot;Estimating&quot;,
AID: null,
}];
const projectDetails2 = [{
a_ID: &quot;B1124-022571-00&quot;,
Start_Date: &quot;2022-05-09T00:00:00.000Z&quot;,
End_Date: &quot;2023-05-02T00:00:00.000Z&quot;,
Project_Type_ID: 12,
Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;,
Active_YN: &quot;Y&quot;,
Committed_Revenue: 3000000,
Revenue: 4000000,
}, {
a_ID: &quot;B1124-022571-11&quot;,
Start_Date: &quot;2022-04-04T00:00:00.000Z&quot;,
End_Date: &quot;2022-12-30T00:00:00.000Z&quot;,
Project_Type_ID: 12,
Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;,
Active_YN: &quot;Y&quot;,
Committed_Revenue: 3000000,
Revenue: 4000000,
}];
const aidBasedMergeDataLookup = new Map(
projectDetails2
.map(({ a_ID, ...mergeData }) =&gt; [a_ID, mergeData])
);
const mergedProjectDetails = projectDetails1
.map(dataItem =&gt; Object.assign(
{}, dataItem, aidBasedMergeDataLookup.get(dataItem.AID)
));
console.log({ mergedProjectDetails });

<!-- language: lang-css -->

.as-console-wrapper { min-height: 100%!important; top: 0; }

<!-- end snippet -->

答案3

得分: 0

创建一个从 projectDetails2 中创建查找对象:

const projectDetails2Lookup = {}
projectDetails2.forEach(item => {
  item = { ...item }
  projectDetails2Lookup[item.a_ID] = { ...item }
  delete item.a_ID
})

然后,如果可能的话,将projectDetails1中的每个元素与查找对象中对应的元素合并:

const finalProjectDetails = projectDetails1.map(item =>
  ({
    ...item,
    ...(projectDetails2Lookup[item.AID] ?? {})
  })
)
英文:

Make a a lookup object out of projectDetails2:

const projectDetails2Lookup = {}
projectDetails2.forEach(item =&gt; {
item = { ...item }
projectDetails2Lookup[item.a_ID] = { ...item }
delete item.a_ID
})

Then merge each element from projectDetails1 with a corresponding element from the lookup object, if possible:

const finalProjectDetails = projectDetails1.map(item =&gt;
({
...item,
...(projectDetails2Lookup[item.AID] ?? {})
})
)

答案4

得分: 0

以下是您请求的中文翻译:

一行代码:使用 Object.assign() 将这两个对象合并成一个新对象。第二个对象是通过 Array::find() 找到的。如果找不到它会返回 undefined 并被 Object::assign() 忽略。

唯一的缺点是 a_ID 会被保留下来,但如果它不被使用,这应该没有问题。否则可以使用 delete 删除它。

如果您想修改第一个数组:

与其他解决方案进行性能基准测试:在JavaScript中通过连接字段合并两个对象。

英文:

A one-liner: use Object.assign() to merge the 2 objects into a new one. The second object is found with Array::find(). If not found it returns undefined and ignored by Object::assign().

The only drawback is that a_ID is left, but if it's not used, that's no problem imho. Otherwise delete it with delete.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const projectDetails1 = [ { ProjectID: 284, ProjectName: &quot;Test Project June-30&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-00&quot;, }, { ProjectID: 285, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-11&quot;, }, { ProjectID: 286, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-22&quot;, }, { ProjectID: 287, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: null, }, { ProjectID: 288, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: null, } ]   
const projectDetails2 = [ { a_ID: &quot;B1124-022571-00&quot;, Start_Date: &quot;2022-05-09T00:00:00.000Z&quot;, End_Date: &quot;2023-05-02T00:00:00.000Z&quot;, Project_Type_ID: 12, Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;, Active_YN: &quot;Y&quot;, Committed_Revenue: 3000000, Revenue: 4000000, }, { a_ID: &quot;B1124-022571-11&quot;, Start_Date: &quot;2022-04-04T00:00:00.000Z&quot;, End_Date: &quot;2022-12-30T00:00:00.000Z&quot;, Project_Type_ID: 12, Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;, Active_YN: &quot;Y&quot;, Committed_Revenue: 3000000, Revenue: 4000000, }, ]; 
const merged = projectDetails1.map(p1 =&gt; Object.assign({}, p1, projectDetails2.find(p2 =&gt; p2.a_ID === p1.AID)));
console.log(&#39;Merged:&#39;, merged)

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; }

<!-- end snippet -->

If you want to modify the first array:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const projectDetails1 = [ { ProjectID: 284, ProjectName: &quot;Test Project June-30&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-00&quot;, }, { ProjectID: 285, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-11&quot;, }, { ProjectID: 286, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-22&quot;, }, { ProjectID: 287, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: null, }, { ProjectID: 288, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: null, } ]   
const projectDetails2 = [ { a_ID: &quot;B1124-022571-00&quot;, Start_Date: &quot;2022-05-09T00:00:00.000Z&quot;, End_Date: &quot;2023-05-02T00:00:00.000Z&quot;, Project_Type_ID: 12, Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;, Active_YN: &quot;Y&quot;, Committed_Revenue: 3000000, Revenue: 4000000, }, { a_ID: &quot;B1124-022571-11&quot;, Start_Date: &quot;2022-04-04T00:00:00.000Z&quot;, End_Date: &quot;2022-12-30T00:00:00.000Z&quot;, Project_Type_ID: 12, Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;, Active_YN: &quot;Y&quot;, Committed_Revenue: 3000000, Revenue: 4000000, }, ]; 
projectDetails1.forEach(p1 =&gt; Object.assign(p1, projectDetails2.find(p2 =&gt; p2.a_ID === p1.AID)));
console.log(&#39;Merged:&#39;, projectDetails1)

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; }

<!-- end snippet -->

Benchmarking with the others solutions:
在JavaScript中通过连接字段合并两个对象。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; }

<!-- language: lang-html -->

&lt;script benchmark count=&quot;10000&quot;&gt;
const projectDetails1 = [ { ProjectID: 284, ProjectName: &quot;Test Project June-30&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-00&quot;, }, { ProjectID: 285, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-11&quot;, }, { ProjectID: 286, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: &quot;B1124-022571-22&quot;, }, { ProjectID: 287, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: null, }, { ProjectID: 288, ProjectName: &quot;Test Project June-28&quot;, Budget: 10000, BudgetMoney: &quot;10,000.00&quot;, BriefStatus: &quot;Approved&quot;, ProjectStatus: &quot;Estimating&quot;, AID: null, } ]   
const projectDetails2 = [ { a_ID: &quot;B1124-022571-00&quot;, Start_Date: &quot;2022-05-09T00:00:00.000Z&quot;, End_Date: &quot;2023-05-02T00:00:00.000Z&quot;, Project_Type_ID: 12, Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;, Active_YN: &quot;Y&quot;, Committed_Revenue: 3000000, Revenue: 4000000, }, { a_ID: &quot;B1124-022571-11&quot;, Start_Date: &quot;2022-04-04T00:00:00.000Z&quot;, End_Date: &quot;2022-12-30T00:00:00.000Z&quot;, Project_Type_ID: 12, Project_Type: &quot;Ext. Delivery (T&amp;M)&quot;, Active_YN: &quot;Y&quot;, Committed_Revenue: 3000000, Revenue: 4000000, }, ]; 
// @benchmark Peter&#39;s solution
const aidBasedMergeDataLookup = new Map(
projectDetails2
.map(({ a_ID, ...mergeData }) =&gt; [a_ID, mergeData])
);
projectDetails1
.map(dataItem =&gt; Object.assign(
{}, dataItem, aidBasedMergeDataLookup.get(dataItem.AID)
));
// @benchmark ray&#39;s solution
const byId = [...projectDetails1, ...projectDetails2].reduce((acc, {a_ID, AID = a_ID, ...other}) =&gt; {
acc[AID] = { AID, ...acc[AID], ...other };
return acc;
}, {});
Object.values(byId);
// @benchmark Alexander&#39;s solution
projectDetails1.map(p1 =&gt; Object.assign({}, p1, projectDetails2.find(p2 =&gt; p2.a_ID === p1.AID)));
&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 11:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601760.html
  • data-structures
  • javascript
  • lookup
  • mapping
  • merge

在Nunjucks模板中访问数组在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定