【发布时间】:2021-07-26 01:48:06
【问题描述】:
我正在尝试将来自 Observables 的多个响应合并到一个流中。我正在获取companies,每个公司都有多个inspections(存储为ID)。对于每个inspection,我必须获取各自的数据。
这就是我的代码当前的样子。请查看注释掉的部分以了解当前结果。
// 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
*/
});
如您所见,我的订阅目前仅返回 Observables。我想我只是错过了合并数据流的部分,我尝试了不同的运算符,如 mergeMap、combineLatest、forkJoin 等,但没有找到合适的。
我想要的输出是这样的(和上面的很像,但是是真实的数据而不是可观察的):
company1:
company_data: ...
inspections: [a, b]
company2:
company_data: ...
inspections: [a, b, c]
有人知道要使用哪个运算符以及如何使用吗? 提前谢谢!
【问题讨论】:
标签: angular typescript rxjs observable rxjs-observables