簡體   English   中英

如何根據從另一個返回的結果中的數組執行另一個 observable

[英]How to execute another observable based on array from result returned from another one

我有一個非常簡單的情況,但我被卡住了。 我有 function customGalleriesHttpService.list()它返回帶有項目數組的可觀察對象。 讓我們假設:

[
{id:1,
... other stuff
},
{id:2,
... other stuff
}
]

現在我想從這個數組中取出每個元素並制作一個額外的 http 請求customGalleriesHttpService.images(data.id) ,它根據他的 id 返回帶有特定元素的圖像數組的 observable。 到目前為止我所取得的成就。

    this.customGalleriesHttpService.list().pipe(
      concatMap((data) => data),
      mergeMap((data) => this.customGalleriesHttpService.images(data.id)),
    ).subscribe((data) => console.log(data));

它正在工作,但還有更多事情要做。 它給了我這樣的 output :

數組[6],數組[6]

我想要實現的是對它進行分組並返回 [Array[6], Array[6]]

在訂閱之前添加一個toArray()運算符應該可以工作

this.customGalleriesHttpService.list().pipe(
  concatMap((data) => data),
  mergeMap((data) => this.customGalleriesHttpService.images(data.id)),
  toArray(), 
).subscribe((data) => console.log(data));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM