I am faced with a scenario where I have two distinct arrays of objects obtained from an aggregate function due to using two different collections.
Although I attempted to utilize the map function as outlined here, it proved unsuccessful in resolving my issue. What other strategies can I employ to achieve the desired outcome?
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
}
]
and
qrySearchLocID = [{
LocalLabID: '123f',
_ID: 'SomePlace1',
AppLabID: 3,
count: 15
},
{
LocalLabID: '12BC',
_ID: 'SomePlace2',
AppLabID: 3,
count: 40
}
];
After attempting the result extraction, only this array is obtained:
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
},
]
To simplify matters, the array has been kept brief. The main requirement is to cross-reference the _ID if it corresponds to sName for the desired outcome:
result = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152,
LocalLabID: '123f',
AppLabID: 3,
count: 15
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156,
LocalLabID: '12BC',
AppLabID: 3,
count: 40
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
}
]
Even though I experimented with this approach:
var result = qrySearch.map((e, _) =>
(_ = qrySearchLocID.find((q) => q._ID=== e.sName)) ?
{ ...e, ...{ _ID: _._ID} } : e);
The outcome lacked the count information.