I have an object with an array of families. Each one of these families holds an array of family member objects. Each of the family members has a contract which is not present right now. My task is to fetch the contract from a rest api and to merge it into the member object.
Object {
content: [{
familiId: '1',
familyMemmbers: [
{ id: '1'
name: 'Tom'
...
},
{ id: '2'
name: 'Peter'
...
}
]
},
{
familiId: '2',
familyMemmbers: [
{ id: '1'
name: 'Paul'
...
},
{ id: '2'
name: 'Joe'
...
}
]
}]
}
this.service.getFamilies().switchMap(familiyPage => from(familiyPage.content))
.switchMap(family => from(family.familyMembers)).mergeMap(member => this.service.getContract(member.id).map(contract => {
return {...member, contract}
My approach returns the object { id: 1, name: 'Tom', contract: ....} on the first emit and { id: 2, name: 'Peter', contract: ....} on the second emit, { id: 1, name: 'Paul', contract: ....} on the third emit and { id: 2, name: 'Joe', contract: ....} on the fourth emit.
What i would like to have is that there is only one emit which contains the whole data structure. i.e.
Object {
content: [{
familiId: '1',
familyMemmbers: [
{ id: '1',
name: 'Tom',
contract: {...},
...
},
{ id: '2',
name: 'Peter',
contract: {...},
...
}
]
}, ...]
}