RXJS:如何将返回一个对象的多个HTTP请求合并到一个数组中

时间:2019-12-16 00:57:14

标签: angular typescript rxjs

您好,我有一个返回object的方法,我计划在最后一次成功调用后调用n次,并combinearray中。 / p>

  public getActivity({minprice, maxprice, type}: QueryParams) {
    const url = `${BASE_URL}?minprice=${minprice}&maxprice=${maxprice}&type=${type}`;
    return this.httpClient.get(url);
  }

我不太确定如何在rxjs中执行此操作,但我的预期输出将是这样的:

activities = [{
  fetchedItemId: 1
},
{
  fetchedItemId: 2
},
{
  fetchedItemId: 3
}
]

1 个答案:

答案 0 :(得分:0)

  getActivity() {
    from(new Array(5))
    .pipe(
      mergeMap(() => this.appService.getActivity(this.filter)),
      filter((activity: Activity) => !activity.error),
      toArray(),
    )
    .subscribe((activities: Activity[]) => {
      this.activities = activities;
    });
  }

我设法通过结合使用pipe,mergeMap,filter和toArray来解决它