RxJs Array of Observable to Array

Springrbua :

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.

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'.  

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

Thierry Templier :

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...

If you want load all the

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 allows you to wait for all observables to have received data.

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];
  (...)
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Observable of array to array (Rxjs)

RxJs Observable in Observable from Array

Simple filter on array of RXJS Observable

rxjs Filter an array of objects observable

RxJs Observable Zip Array of Observables

RxJS observable of array not working as expected

RxJS - Test for empty Observable array

RxJS/Observable flatMap can return Observable or array

RxJS append observable to observable array list in typescript

How to turn an observable of an array into an array of observables (RxJS)

Angular - rxjs - Transform Observable array to another Observable array

How to merge Array Observavble of array in only one Observable of array with RXJS?

RxJS 6 filter and map an observable array of items

Angular RxJS Observable Filter on array of objects

RxJs how to map the items in an Observable<Array>

Observable Array of Observables - How to implement in RxJS?

Best way to "flatten" an array inside an RxJS Observable

How to add item in rxjs observable array in angular

rxjs6 filter Observable<Array<any>>

RxJS: chunk and delay on a large observable array

Observable of array with filter using Angular 5 with RxJS

RXJS - Multiple observable calls on array of properties

Rxjs Observable run sequentially and return an array

RXJS Observable Transform Array To Multiple Values

RxJS Observable: Emit every specific change in the array

How to push to Observable of Array in Angular 4? RxJS

RxJS 6 get a filtered list of array of Observable

RxJS groupBy on ngrx effects to observable custom array

Modify Observable array before subscribing by rxjs operators

TOP Ranking

HotTag

Archive