Transform data from one observable using another observable in angular rxjs

Mr.Milannist

I have two observables: cities and addresses. In my addresses array I have an object with a property city_code which has a value and a property city which is empty. In cities I have objects with both properties.

How can I transform the data in addresses? I want to get the value for city from the cities array by matching the city_code and put that value into the corresponding object in addresses.

addresses$ = this.store.select(getAddresses);
cities$ = this.citiesFacade.allCities$;

function getAddressesWithCityName() {
   // some code here
}

Address and City:

export interface Address {
   city_code: string;
   city: string; // in addresses$ this property is empty and I should get it from cities$
}

export interface City{
   city_code: string;
   city: string;
}
BizzyBob

You can use combineLatest() to create an observable stream that is always up to date based on multiple source streams.

Then, you can simply define addresses$ as follows, without the need for the getAddressesWithCityName() method:

addresses$ = combineLatest([this.store.select(getAddresses), this.cities$]).pipe(
    map(([addresses, cities]) => addresses.map(a => {
        const city = cities.find(c => c.city_code === a.city_code)?.city;
        return { ...a, city };
    })
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using MergeMap with the array of data received from another observable - RxJs Angular

how to change the data of the first observable using data from another observable including the condition in angular/rxjs

Angular - rxjs - Transform Observable array to another Observable array

Using data from another observable

Angular RxJS add data from new observable to current observable

Rxjs One Observable Feeding into Another

Use data from one observable in another and then return result as other observable

Returning a Property from Observable as Observable in Rxjs Angular

Rxjs: how to switch from one observable to another mid stream

Angular RxJS Copy Value from One Element to Next in Observable Array

Using one observable to synchronize another observable

Angular how do I filter the data from an observable using rxjs pipe

Angular2 and RxJS : Replace Observable Response by another Observable Response using Map

Angular 5 / Typescript/RxJS - get data from Observable

How to store observable data from API with rxJS in Angular 5?

Angular - How to return an observable from another observable

Angular7 service - returning observable that contains the data from another observable but subscribing to that second Observable within component?

Angular/RxJS - Converting a promise and inner observable to one single observable to be returned

Angular / RxJS Multicasting Observable of Observable

RxJs: Filter content in observable using emission of another observable

RxJS: Setting default value for observable from another observable

What is the proper method for using the output of one observable as the input to another in angular

Using Observable data from one ngrx store reducer to filter results in another

RxJs Observable in Observable from Array

Angular RxJs Observable stream merge data

How to update data in Observable in Angular? rxjs

How to transform properties in a RXJS Observable

Observable of array with filter using Angular 5 with RxJS

Observable not subscribing when using pipe in Rxjs - Angular