简体   繁体   English

结合来自多个rxjs可观察量的结果

[英]combining results from multiple rxjs observables

I have an autocomplete input which, as the user types, fetches data from multiple endpoints, such as: 我有一个自动完成输入,当用户输入时,它从多个端点获取数据,例如:

//service call to fetch data and return as single observable
getAutocompleteSuggestions() {
    const subs$ = [
        this.http.get(endpoint1),
        this.http.get(endpoint2),
        this.http.get(endpoint3)
    ];

    return Observable.forkJoin(...subs$);
}

Each of these endpoints returns data of the form: 每个端点都返回以下形式的数据:

{ data: [], status: xyz }

I would like to use switchmap as I want to only show results from the final call, and have tried the following: 我想使用switchmap,因为我只想显示最终调用的结果,并尝试了以下内容:

   this.getAutocompleteSuggestions(query)
          .switchMap(res => {
             return res.data;
           })
          .subscribe((results: any) => {
            this.results = results;
          });

But the 'res' in the switchmap is an array, any idea how results can contain a single array containing the data from the response of any number of observables? 但是switchmap中的'res'是一个数组,任何想法结果如何包含一个包含任意数量的observable响应数据的单个数组?

I don't fully understand what you want, but I think this is it: 我不完全明白你想要什么,但我认为这是:

$filter: Subject<string> = new Subject(); //I guess we have some value to filter by??

Push a value to the subject: 将值推送到主题:

this.$filter.next(myNewValue);

In the constructor or init: 在构造函数或init中:

this.$filter
   .switchMap(filterValue => { //Get the values when filter changes
       subs$ = [
         this.http.get(endpoint1 + filterValue),
         this.http.get(endpoint2 + filterValue),
         this.http.get(endpoint3 + filterValue)
       ];

       return Observable.forkJoin(...subs$);
   })
   .map(results => { //now map you array which contains the results
      let finalResult = [];
      results.forEach(result => {
          finalResult = finalResult.concat(result.data)
      })
      return final;
   })
   .subscribe(); //Do with it what you want

The entire steam will be executed again when we put a new value into our subject. 当我们为我们的主题添加一个新值时,整个蒸汽将再次执行。 SwitchMap will cancel all ready requests if there are any. 如果有的话,SwitchMap将取消所有就绪请求。

I have used something similar to this, and worked well so far: 我使用了类似的东西,到目前为止运作良好:

        let endpoint1 = this.http.get(endpoint1);
        let endpoint2 = this.http.get(endpoint2);
        let endpoint3 = this.http.get(endpoint3);

        forkJoin([endpoint1, endpoint2, endpoint3])
          .subscribe(
            results => {
              const endpoint1Result = results[0].map(data => data);
              const endpoint2Result = results[1].map(data => data);
              const endpoint3Result = results[2].map(data => data);

              this.results = [...endpoint1Result, ...endpoint2Result, ...endpoint3Result];
            },
            error => {
              console.error(error);
            }
          );

Obviously is a pretty simple example, you'll be able to handle better the results to suit your needs. 显然这是一个非常简单的例子,您将能够更好地处理结果以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rxjs将来自2个不同可观察对象的2个对象组合在一起 - rxjs combining 2 objects together from 2 different observables RxJS结合可观察物 - RxJS Combining observables 将rxjs中的多个observable组合到一个缓冲区中 - Combine multiple observables from rxjs into a single buffer 取消订阅RxJS Observables - Unsubscribe from RxJS Observables Angular - 链接 Observables 并合并它们的结果 - Angular - chaining Observables and combining their results RXJS:为什么 mergemap 在示例中不提供来自多个内部可观察对象的输出? - RXJS: Why is mergemap not providing output from multiple inner observables in example? 在RxJS中组合不同类型的Observable,但只发出来自其中一个的值 - Combining different kinds of Observables in RxJS but only emitting values coming from one of them 将可观察对象串联和并行组合以从多个 API 获取数据 - Combining observables in series & parallel to fetch data from multiple APIs Rxjs - 组合2个Observable并发出组合结果不起作用 - Rxjs - Combine 2 Observables and emit combined results not working RxJ:一个接一个地执行3个可观察对象,并使用第一个请求,第二个请求和第三个请求的结果 - RxJs: Executing 3 observables one after another and using results from first in second, and first and second in third requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM