How to combine two object lists into one list by id in rxjs using observables?

Issue

This Content is from Stack Overflow. Question asked by Cronos

I have two service calls that return with an observable list:

service.getAllUsers(): observable<User[]> where User looks like this:

    User {
     id: number,
     username: string,
     ...
    }

and the other service call:

service.getSomeData() : Observable<Data[]>

 Data {
  userId: number,
  userSomeData1,
  userSomeData2,
  ...
}

I want to merge these two lists into one Observable list, based on userId:

Observable<Result[]>

where Result should looks like this:

 Result {
  id,
  username,
  userSomeData1,
  userSomeData2,
  ...
}

What I have done so far:

const result$ = this.userService.getAllUsers()
                .pipe(switchMap(users => {
                   return this.userService.getSomeData()
                    .pipe(map(data => ({users, data})))
                }))

But its not what I want.
Please help me, how can I achive this?



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?