[英]using RxJs forkJoin to merge multiple api calls into single output array
假設我有 4 個 API 調用。 在這種情況下,4 個調用中的每一個都返回相同的數據結構,因此我不需要操作返回類型。 它們每個都返回一個對象數組。 我想將它們全部稱為,但將它們組合到 output 陣列中。 我嘗試將forkJoin
與.concat(
) 結合起來,但這並沒有達到我的預期(它將它們放在較小的數組中)。
forkJoin([
this.service.getData('something', 'somethingelse'),
this.service.getData('something1', 'somethingelse2')
].concat()).subscribe(data => console.log(data))
考慮以下實現:(在返回數據后執行連接 arrays)
forkJoin([
this.service.getData('something', 'somethingelse'),
this.service.getData('something1', 'somethingelse2')
])
.pipe(
map(x => x.reduce((arr, curr) => [...arr,...curr]))
)
.subscribe(data => console.log(data))
或者使用ES2019 flat
forkJoin([
this.service.getData('something', 'somethingelse'),
this.service.getData('something1', 'somethingelse2')
])
.pipe(
map(x => x.flat())
)
.subscribe(data => console.log(data))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.