angular - Map Observable of Array using Observable
问题描述
I try to find a way to get this scenario working :
- I have an observable
talks$
which emits a list of all talks. - A talk have a property
personId
- This property can be mapped to the person using an Observable.
- I try to map it into a class
TalkWithPerson
const test = this.talks$.pipe(
exhaustMap(talks =>
talks.map(t => this.getPersonById(t.personId).pipe(map(p => newTalkWithPerson(t, p))))
),
);
Currently, this emits 2 observable, each one emitting my TalkWithPerson
object. (Observable<Observable<TalkWithPerson>>
)
I would like to have an Observable<TalkWithPerson[]>
if possible.
I was thinking going the hard way with getting all the people and all the talks and use combineLatest
with a project function to match the records but I don't want to load all the persons, it will cause to load a huge list...
Thank you for your help !
StackBlitz : https://stackblitz.com/edit/talks-person
解决方案
尝试这个:
const test = this.talks$.pipe(
exhaustMap(talks =>
from(talks)
.pipe(
mergeMap(t => this.getPersonById(t.personId).pipe(map(p => newTalkWithPerson(t, p)))),
toArray())
)
),
);
- 导入
from
为创建方法import { from } from "rxjs";
- 导入
toArray
与“地图”可管道操作符相同
推荐阅读
- php - 从 Flipkart 订单 api v2 获取订单的发货详细信息
- c++ - 为什么一个对象不构造?
- java - 如何在带有集合的参数上使用休眠搜索条件
- wpf - 如何创建透明的 WPF 窗口?
- github - Github 检查 + 动作集成
- machine-learning - 贝叶斯优化不能提高预测精度
- python-3.x - Scikitlearn Column Transformer Error: Column ordering must be equal for fit and for transform when using the remainder keyword
- python - 在 AWS Lambda 中使用 Python SQLAlchemy 是个好主意吗?
- ruby-on-rails - 将 Rails 中 select_tag 中的参数传递给一个 script.rb 获取此参数
- css - 为什么 a:hover 不适用于引导程序源?