Shohel
Shohel

Reputation: 3934

merge two observables array into single observable array

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.

Upvotes: 0

Views: 3126

Answers (2)

R3tep
R3tep

Reputation: 12854

You can use merge to merge multiple observable.

e.g.

import { merge } from 'rxjs';

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

Upvotes: 0

wentjun
wentjun

Reputation: 42516

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);

Upvotes: 1

Related Questions