I'm trying to merge multiple responses from Observables to one stream. I'm fetching companies
and each company has multiple inspections
(which are stored as IDs). For each inspection
I have to fetch the respective data.
This is how my code currently looks like. Please check the commented out section for the current result.
// Get companies
getCompanies().pipe(
switchMap(companies => companies.map(
company => {
return company['inspections'].map(inspection => {
// Get inspections by each company
return this.getInspectionById(inspection['id']);
})
}
))
).subscribe(result => {
/*
console.log(result) returns the following (company1 has two inspections, company2 has three
inspections):
(2) [Observable, Observable]
0: Observable
1: Observable
(3) [Observable, Observable, Observable]
0: Observable
1: Observable
2: Observable
*/
});
As you can see, my subscription currently returns the Observables only. I guess I'm just missing the part for merging the data streams, I've tried different operators like mergeMap, combineLatest, forkJoin etc. but didn't found the right one.
My desired output would be something like this (quite the same as above, but real data instead of observables):
company1:
company_data: ...
inspections: [a, b]
company2:
company_data: ...
inspections: [a, b, c]
Does anybody know which operator to use and how to do it? Thank you in advance!