Rxjs - Multiple sources for .withlatestfrom

Preda70R picture Preda70R · Apr 19, 2017 · Viewed 14.6k times · Source

I want to merge the latest emitted values from multiple observables, so right now I'm using .withLatestFrom. Except, it nests the data together in nested arrays instead of pushing the data into a new array value. Example code below.

Any ideas how I can retrieve multiple observable emits using .withLatestFrom?

.withLatestFrom(source1)
.withLatestFrom(source2)
.withLatestFrom(source3)
.map((data) => { console.log(data) });

Answer

adharris picture adharris · Apr 19, 2017

withLatestFrom supports multiple observables:

.withLatestFrom(source1, source2, source3)
.map((data) => { console.log(data) });

-> [val, value1, value2, value3]

It also supports function as it's last parameter, so you can get values other than arrays:

observable$
    .withLatestFrom(source1, source2, (val, one, two) => {
        return {val: val, one: one, two: two};
     });