I have two arrays that I want to join conditionally:
aData = [ { id: 1, title: `bla`}, { id: 2, title: `la`}, { id: 3, title: `lala`} ]
bData = [ { id: 1, description: `bla`} ]
In the first case I want to be able to get only matching result (aData+bData
) with merged properties
matchingIdsResult = [ { id: 1, title: `bla`, description: `bla` } ]
IN the second case I want the unmatching result - just objects from aData
with ids that didn't ocour in bData
:
unmathingIdsResult = [ { id: 2, title: `la`}, { id: 3, title: `lala`} ]
I was playing with .map
and .reduce
and achieve this so far:
const data: any = aData.concat(bData).reduce((acc, x) => {
acc[x.id] = Object.assign(acc[x.id] || {}, x);
return acc;
}, {});
However in that case I do get all aData
objects merged with matching bData
properties so far which is not really what I want.