Alexander Mills
Alexander Mills

Reputation: 100486

Merge/concat to array

I have this right now:

import {concat, Observable, ReplaySubject, pipe, merge} from 'rxjs';
import {takeUntil, takeWhile, toArray} from 'rxjs/operators';


const rs1 = new ReplaySubject(1);  
const rs2 = new ReplaySubject(1);
const rs3 = new ReplaySubject(1);

rs1.next(1);
rs2.next(2);
rs3.next(2);

merge(rs1,rs2,rs3).pipe(toArray()).subscribe(v => {
  console.log({v});
});

right now it's logging nothing, but I am looking to get this logged:

{v: [1,2,3]}

how do I concat/merge them to an array?

Upvotes: 0

Views: 196

Answers (1)

Maksim Romanenko
Maksim Romanenko

Reputation: 365

Then you should use zip instead of merge.

import {ReplaySubject, zip} from 'rxjs';


const rs1 = new ReplaySubject(1);  
const rs2 = new ReplaySubject(1);
const rs3 = new ReplaySubject(1);

rs1.next(1);
rs2.next(2);
rs3.next(2);

zip(rs1,rs2,rs3).subscribe(v => {
  console.log({v});
});

Upvotes: 2

Related Questions