Two get requests are returning the following data:
[{
id: 1,
hId: 2
}, {
id: 6,
hId: 1
}]
The second request returns:
[{
id: 1,
name: 'Bob'
}, {
id: 2,
name: 'Billy'
}, {
id: 6,
name: 'John'
}]
The objective using RxJs and observables is to achieve this structure:
[{
id: 1,
name: 'Bob',
married: true // if there's an object with hId equal to id
}, {
id: 2,
name: 'Billy',
married: true // if there's an object with hId equal to id
}, {
id: 6,
name: 'John',
married: false
}]
The current approach involves using forkJoin, but it requires iterating over responses[0] within map and filtering/searching for elements within subjects. Is there a more efficient way to achieve this using one of the rxjs operators/functions?
const joint = forkJoin([mariage, subjects]);
return joint.pipe(
map(responses => {
responses[0].data.forEach((mItem) => {
const found = responses[1].data.find((subj) => {
return subj.id === mItem.hId;
});
mItem.married = found ? true : false;
});
console.log(responses[0].data);
return responses[0].data;
})
);