Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TS: how to merge values in an array of objects into one array

I call the function

getAllUsers() {
    return this.http.get(this.url);
}

with:

this.UserService.getAllUsers().subscribe(res => {
   console.log(res);
})

The output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{id:1, name:'anna'}, {id:2, name:'john'}, {id:3, name:'victor'}]

but I would like to return an array of all names:

['anna', 'john', 'victor']

Does anyone have an idea how to do that?

>Solution :

this.UserService.getAllUsers().subscribe(res => {
   const names = res.map(i => i.name)
   console.log(names)
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading