0
votes

Say I have the following 2 objects:

obj1 = {
  dataId: 1,
  item: {}
} 

obj2 = {
  id: 1,
  data: { a: 1, b: 2, c: 3 }
}

I then have 2 observables that hold arrays of objects of the above types:

Observable<obj1[]>
Observable<obj2[]>

Is there a way using rxjs to merge the 2 observables into a new observable where if the Ids are equal to each other then the data of obj2 is mapped to the item field in object 1? So going of the objects above, results in an observable with this object:

newObj = {
  id: 1,
  item: { a: 1, b: 2, c: 3 }
}

The second observable will always contain a matching id for the first observables dataId field, and there may be many dataIds of the same value but the ids in the second observable will always be unique.

1

1 Answers

1
votes

You can do this in RxJS but you'll need to implement the matching part yourself. Using combineLatest (https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/combinelatest.md) you can combine the two streams. In the resultSelector function, you could write the logic to actually merge the two arrays.

// pseudo code

 combineLatest([firstObs$, secondObs$], ([first, second]) => {
  // write the combining logic here and return the result
 });