merge two observables array into single observable array

时光怂恿深爱的人放手 提交于 2019-12-12 06:39:06

问题


I have a Observable array similarIdeasObservable$ and another Observable array being getting from server by like this.ideaService.getSimilarIdeas(). I want to merge these two Observable arrays without using subscribe. How Can I do this?

I am trying following way

 this.similarIdeasObservable$.pipe(concat(this.ideaService.getSimilarIdeas(this.idea).pipe(
        concatMap(i => i),
        toArray(),
        catchError(error => {
          throw error;
        }),
        finalize(() => {
          this.loading = false;
        })
      )));

concat is deprecated.


回答1:


You can use merge to merge multiple observable.

e.g.

import { merge } from 'rxjs';

const example = merge(
  similarIdeasObservable$,
  this.ideaService.getSimilarIdeas(this.idea)
);



回答2:


I would recommend you to use forkJoin.

The idea of how forkJoin works is that it requires the input observables (observableA and observableB on the below example) to be completed, and it will eventually be used to return an observable represented by an array, which consists of the values returned by the input observables.

import { forkJoin } from 'rxjs';

const observableA = this.similarIdeasObservable$;
const observableB = this.ideaService.getSimilarIdeas(this.idea);

forkJoin(observableA, observableB);


来源:https://stackoverflow.com/questions/57178820/merge-two-observables-array-into-single-observable-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!