首页 > 解决方案 > Map Observable of Array using Observable

问题描述

I try to find a way to get this scenario working :

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

标签: angularrxjs

解决方案


尝试这个:

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与“地图”可管道操作符相同

推荐阅读