2017-07-26 65 views
0

我一直在试图解决这个问题一段时间了,我猜这是一个相当普遍的问题,但我想要做的是增加来自观察到,并增强与观察或promiselike 发生所以我想类似如下:在可观察的数组中增加单独的值 - RxJS

function augment(person: Person): Observable<PersonWithAddress> { 
    // does ajax call or something 
} 

const items$: Observable<Person[]>; 

items$ 
    .do(x => {}) // x would be of type Person[] 
    .flatFlatMap(person => { // person would be of type Person 
     return augment(person); // this would return an Observable<PersonWithAddress> 
    }) 
    .subscribe(peopleWithAddresses => { // peopleWithAddresses would be of type PersonWithAddress[] 
    }) 

是否有某种运营商对于这一点,我得到我可以增加或映射从可观察到的发射的单个项目来自flatMap可观察对象的其他东西,但是有一些像flatFlatMap等等。

回答

1

您可以使用forkJoin做你想要什么。

它需要可观测量(或承诺),并且当所有已完成(或解决)的阵列,其发出包含上次发射(或解决)值的数组:

import { Observable } from "rxjs/Observable"; 
import "rxjs/add/observable/forkJoin"; 


items$ 
    .flatMap((people: Person[]) => Observable.forkJoin(
    people.map(person => augment(person)) 
)) 
    .subscribe((peopleWithAddresses: PersonWithAddress[]) => { 
    // ... 
    }); 
+0

OMG,非常感谢,我现在一直在为此苦苦挣扎。有这么多的经营者 – YentheO

+1

是的,有几个学习。您可能没有看到它,但RxJS网页底部有一个向导,您可能会发现它很有用:http://reactivex.io/rxjs/ – cartant