RxJs数组可观察到的数组 [英] RxJs Array of Observable to Array

查看:277
本文介绍了RxJs数组可观察到的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于用Angular2在TypeScript中编写的Web应用程序,我需要使用RxJs Observable s.
由于我以前从未使用过rxjs,而且我一般都不熟悉反应式编程,所以有时我会遇到一些困难,难以找到正确的方法来做一些特定的事情.
............................................................................................................................. 我现在面临一个问题.我必须将Array<Observable<T>>转换为Observable<Array<T>>.
我将尝试通过一个示例进行说明:
-我有一个Observable,它为我提供了Users(Observable<Array<User>>)
的列表 -User类具有返回Observable<Array<Post>>的函数getPosts.
-我需要将Observable<Array<User>>映射到Observable<Array<Post>>,以评估onNext函数中的所有Post.

For a Web-Application written with Angular2 in TypeScript, I need to work with RxJs Observables.
As I never used rxjs before and I am new to reactiveprogramming in general, I sometimes have some difficulties finding the right way to do some specific things.
I am now facing a problem. where I have to convert an Array<Observable<T>> into an Observable<Array<T>>.
I'll try to explain it with an example:
- I have an Observable, which gives me a list of Users (Observable<Array<User>>)
- The User-class has a function getPosts returning an Observable<Array<Post>>.
- I need to map the Observable<Array<User>> to an Observable<Array<Post>> to evaluate all the Posts inside the onNext function.

我可以使用
轻松地将Observable<Array<User>>映射到Observable<Array<Observable<Array<Post>>>> map((result : Array<User>) => result.map((user : User) => user.getPosts()))
我可以将Array<Array<Post>>展平为Array<Post>.
但是我只是找不到将Observable<Array<Observable<Array<Post>>>>映射到Observable<Array<Array<Post>>>
的正确方法 到目前为止,我将combineLatest函数与flatMap一起使用.
对我来说,它似乎可以正常工作,而且我使用的编辑器(Atom编辑器)没有显示任何错误.但是,现在我使用Netbeans,这向我显示了此代码中的错误.同样使用"tsc"编译代码也会导致错误.
外观如下:

I can easily map from Observable<Array<User>> to Observable<Array<Observable<Array<Post>>>> using
map((result : Array<User>) => result.map((user : User) => user.getPosts()))
ans I can flatten an Array<Array<Post>> into an Array<Post>.
However I just can't find the correct way to map the Observable<Array<Observable<Array<Post>>>> into an Observable<Array<Array<Post>>>
Until now i used the combineLatest function together with flatMap.
To me it seemed to work and the editor i used (Atom editor) did not show any error. However now I use Netbeans, which shows me an error in this code. Also compiling the code using "tsc" results in errors.
The look like this:

Argument of type '(result: Post[]) => void' is not assignable to parameter of type 'NextObserver<[Observable<Post>]> | ErrorObserver<[Observable<Post>]> | CompletionObserver<[...'.
Type '(result: Post[]) => void' is not assignable to type '(value: [Observable<Post>]) => void'.  

所以我的问题是:
如何将ObservablesArray扁平化为Array?

So my question is:
How can I "flatten" an Array of Observables into an Array?

推荐答案

flatMap运算符允许执行此操作.我不完全了解您要做什么,但我会尽力提供答案...

The flatMap operator allows to do that. I don't fully understand what you try to do but I'll try to provide an answer...

如果要加载所有

getPostsPerUser() {
  return this.http.get('/users')
    .map(res => res.json())
    .flatMap((result : Array<User>) => {
      return Observable.forkJoin(
        result.map((user : User) => user.getPosts());
    });
}

Observable.forkJoin使您可以等待所有可观测对象接收到数据.

Observable.forkJoin allows you to wait for all observables to have received data.

上面的代码假定user.getPosts()返回一个可观察的...

The code above assumes that user.getPosts() returns an observable...

有了这个,您将收到一连串的帖子:

With this, you will receive an array of array of posts:

this.getPostsPerUser().subscribe(result => {
  var postsUser1 = result[0];
  var postsUser2 = result[1];
  (...)
});

这篇关于RxJs数组可观察到的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆