Reputation: 375
I have an Angular application that uses NGRX to manage the application state. I have a selector that return me an Observable.
export const mySelector = createSelector(state, (state: IState) => state.field.array);
The Observable returned has this format: {name:string; age:number}[]
What I need to do is to turn that data format in another data format, for example {firt-name:string}
.
I can't understand what operator should I use.
My goal is to assign the new data to a variable, inside the subscribe, somethig like this:
myVariable: {firt-name:string};
this.store.pipe(select(mySelector))
.pipe(//here I should transform the data format)
.subscribe(result => {myVariable = result})
Upvotes: 2
Views: 332
Reputation: 13071
You can either use map
:
function mapper(data: { name: string; age: number }[]) {
return data.map(({ name, age }) => ({
"first-name": name,
age
}));
}
this.store
.pipe(
select(mySelector),
map(mapper)
)
.subscribe(result => {
myVariable = result;
});
or perform that transformation in your selector:
function mapper(data: { name: string; age: number }[]) {
return data.map(({ name, age }) => ({
"first-name": name,
age
}));
}
this.store
.pipe(
select(
pipe(
mySelector,
mapper
)
)
)
.subscribe(result => {
myVariable = result;
});
Upvotes: 2