How can I concatenate results of two observables using mergemap

55 Views Asked by At

Got stuck on mergemap while learning rxjs. I have 2 observables

let list1= of(1,2)
let list2= of('A','B')

and I want to concatenate the results like this:

1A, 1B, 2A, 2B

How to accomplish the concatenation inside mergemap block

 return list1.pipe(
     mergeMap((item: any) => {
           i want to concatenate items of list1 with items of list2 here 
     })
 ).subscribe(console.log)

I appreciate your help with this please

I have tried

 return list1.pipe(
     mergeMap((item: any) => {
           return item + list2  
     })
 ).subscribe(console.log)

But it returns 1 [ o b j e c t ] 2 [ o b j e c t ]

1

There are 1 best solutions below

3
On BEST ANSWER

Use an inner pipe

import { of, map, mergeMap } from 'rxjs';

const list1 = of(1, 2);
const list2 = of('A', 'B');

list1
  .pipe(
    mergeMap((list1Value) =>
      list2.pipe(map((list2Value) => list1Value + list2Value))
    )
  )
  .subscribe(console.log);

Stackblitz: https://stackblitz.com/edit/rxjs-2zsk7b?file=index.ts