I'm trying to merge 2 object arrays (each one I get from different apis and are of different types). The merge will be done on the last method but first I need to get both lists out of their observables. I need to do something like this:
this.result$ = this.firstValues$.pipe(
function((firstValues) => firstValues.map(firstValue => this.secondApi.fetch(firstValue.id))),
function((firstValues, secondValues) => this.fillFirstWithSecond(firstValues, secondValues))
);
Any ideas?
EDIT #1:
The function this.secondApi.fetch
returns an Observable<SecondValue>
.
The question is quite obtuse. I assume by function you mean RxJS operators. In that case, you'd need to make quite a few changes. Although it would be much simpler to also attach a sample input with expected output.
this.result$ = this.firstValues$.pipe(
switchMap((firstValues) =>
forkJoin( // <-- trigger requests in parallel
firstValues.map(firstValue => // <-- JS `Array#map` function
this.secondApi.fetch(firstValue.id).pipe(
map(secondValue => ({ // <-- include the `firstValue` in `secondApi`'s response
firstValue: firstValue,
secondValue: secondValue
}))
)
)
)
),
map((values: {firstValue: any, secondValue: any}[]) => {
this.fillFirstWithSecond(values.firstValues, values.secondValues)
})
);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments