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

Viewed 21

I call the function

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

with:

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

The output is:

[{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?

1 Answers
this.UserService.getAllUsers().subscribe(res => {
   const names = res.map(i => i.name)
   console.log(names)
})
Related