gpt4 book ai didi

javascript - 如何根据 Javascript 中的属性组合两个不同大小的对象数组?

转载 作者:行者123 更新时间:2023-11-29 11:00:11 25 4
gpt4 key购买 nike

我有两个长度不同但共享相似信息的对象数组。

qrySearchLocID = [{
LocalLabID: '123f',
SystemID: 5000152,
AppLabID: 3
},
{
LocalLabID: '12BC',
SystemID: 5000384,
AppLabID: 3
},
];

qrySearch = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
},
]

我想根据 SystemID 组合这两个数组,从 qrySearch 复制所有信息并从 qrySearchLocID 添加 LocalLabID,仅此而已。例如我希望结果数组是

[{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152,
LocalLabID: '123f'
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384,
LocalLabID: '12BC'
},
]

提前致谢。

最佳答案

您可以使用mapfind 函数。

var qrySearchLocID = [{
LocalLabID: '123f',
SystemID: 5000152,
AppLabID: 3
},
{
LocalLabID: '12BC',
SystemID: 5000384,
AppLabID: 3
},
];

var qrySearch = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
},
];

var result = qrySearch.map((e, _) =>
(_ = qrySearchLocID.find((q) => q.SystemID === e.SystemID)) ?
{ ...e, ...{ LocalLabID: _.LocalLabID } } : e);

console.log(result);

资源

关于javascript - 如何根据 Javascript 中的属性组合两个不同大小的对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48571919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com