Home > Net >  RxJs how to map an aray of objects to array of string?
RxJs how to map an aray of objects to array of string?

Time:01-06

I am beginner in RxJs, please advice me how to transform a list of objects in a list of its names, like here:

import { map } from 'rxjs/operators';

var names = this.http.get<Project[]>('/api/projects').pipe(map(p=> p.name));

it says name does not exist on the Project[]...

CodePudding user response:

It's a valid error since p in your code is an array. To achieve what you want use map method:

this.http.get<Project[]>('/api/projects').pipe(
  map(projects => projects.map(project => project.name))
);

CodePudding user response:

map oprator is not iterating through your Projects array. It stands between observable stream value and returns what you mutate or want with data . So you need to perform a js map inside Map oprator to mutate data to what you want

 var names = this.http.get<Project[]> 
  ('/api/projects').pipe(map(projects=> projects.map(p=> // whatever you wana do)))
  •  Tags:  
  • Related