RxJs Array of Observable to Observable of Array

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("https://stackoverflow.com/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];
  (...)
});

Leave a Comment