最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Rxjs Forkjoin, how do I return single array for a particular property? - Stack Overflow

matteradmin10PV0评论

I have a fork join that grabs data from 10 http sources, and works well. The structure of each source is the same. However, the results i'm looking for are in the "data" property of the response. I need to get all the results into a single array of objects.

Response

{
 data:[stuff],
 included: [stuff],
 version: 1.0
}

Code

 forkJoin(requestArray).subscribe(results => {
   console.log(results);
 });

How can I return the result of this forkjoin, merging all of arrays from in the data property from every response into a single array?

Desired Output

[{all-Data-Objects-In-An-Array}, ...]

I have a fork join that grabs data from 10 http sources, and works well. The structure of each source is the same. However, the results i'm looking for are in the "data" property of the response. I need to get all the results into a single array of objects.

Response

{
 data:[stuff],
 included: [stuff],
 version: 1.0
}

Code

 forkJoin(requestArray).subscribe(results => {
   console.log(results);
 });

How can I return the result of this forkjoin, merging all of arrays from in the data property from every response into a single array?

Desired Output

[{all-Data-Objects-In-An-Array}, ...]
Share Improve this question edited May 22, 2020 at 16:24 Troyd asked May 22, 2020 at 16:11 TroydTroyd 4345 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
import { Component } from "@angular/core";
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/timer";
import "rxjs/add/operator/do";
import "rxjs/add/operator/take";
import "rxjs/add/operator/share";
import "rxjs/add/operator/shareReplay";
import { forkJoin } from "rxjs/observable/forkJoin";
import { of } from "rxjs/observable/of";
import { tap, map } from "rxjs/operators";

@Component({
  selector: "my-app",
  templateUrl: "./app.ponent.html",
  styleUrls: ["./app.ponent.css"]
})
export class AppComponent {
  name = "Angular 5";
  data1 = {
    data: ["abc"],
    version: 1.0
  };
  data2 = {
    data: ["abc"],
    version: 1.0
  };
  private init$: Observable<any>;

  public ngOnInit() {
    forkJoin(of(this.data1), of(this.data2))
      .pipe(
        map(results => results.reduce((all, itm) => all.concat(itm.data), []))
      )
      .subscribe(results => {
        console.log(results);
      });
  }
}

just add a map operator and run a reduce inside it...

forkJoin(requestArray).pipe(
  map(results => results.reduce((all, itm) => all.concat(itm.data), []))
).subscribe(results => {
   console.log(results);
 });
Post a comment

comment list (0)

  1. No comments so far