admin管理员组文章数量:1435859
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
2 Answers
Reset to default 5import { 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);
});
本文标签: javascriptRxjs Forkjoinhow do I return single array for a particular propertyStack Overflow
版权声明:本文标题:javascript - Rxjs Forkjoin, how do I return single array for a particular property? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673971a2669738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论