I have two arrays of objects that are different in length but share similar information.
qrySearchLocID = [{
LocalLabID: '123f',
SystemID: 5000152,
AppLabID: 3
},
{
LocalLabID: '12BC',
SystemID: 5000384,
AppLabID: 3
},
];
and
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
},
]
I want to combine these two arrays based on the SystemID, copy all the information from qrySearch and add the LocalLabID from qrySearchLocID and nothing else. For example I want the result array to be
[{
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'
},
]
Thanks in advance.
You can use map
and find
functions.
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);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments