0

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[]...

2 Answers 2

3

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))
);
Sign up to request clarification or add additional context in comments.

Comments

1

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)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.