我有两个长度不同但共享相似信息的对象数组。
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'
},
]
提前致谢。
答案 0 :(得分:2)
您可以使用map
和find
功能。
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);

答案 1 :(得分:0)
我的建议是将两者都转换为由SystemID键入的对象文字的中间形式。然后,您可以将所有属性添加到该对象,然后再转换回您需要的任何长期形式。
答案 2 :(得分:0)
您可以使用Array.findIndex
函数在符合特定条件的数组中查找索引,然后将信息添加到该索引处的对象:
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
},
]
for(var i = 0; i < qrySearch.length; i++){
var j = qrySearchLocID.findIndex(function(elem){
return elem.SystemID == qrySearch[i].SystemID;
});
if(j != -1){
qrySearch[i].LocalLabID = qrySearchLocID[j].LocalLabID;
}
}
console.log(qrySearch)
&#13;
答案 3 :(得分:0)
要加快查找速度,您可以先将第一个数组转换为由SystemID键入的Map。然后在第二个数组的map
中使用它,在地图中查找SystemID并获取它的LocalLabID(如果存在)。
在这里,您可以看到一个功能表达式中发生的一切:
const 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 }];
const result = qrySearch.map(
(map => o => Object.assign(o, map.has(o.SystemID)
&& { LocalLabID: map.get(o.SystemID) }))
(new Map(qrySearchLocID.map(loc => [loc.SystemID, loc.LocalLabID])))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }