-1

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?

kelsny
  • 23,009
  • 3
  • 19
  • 48
user16405471
  • 61
  • 1
  • 7
  • 1
    Have a look at [Array.prototype.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). – kelsny Sep 13 '22 at 15:18

1 Answers1

1
this.UserService.getAllUsers().subscribe(res => {
   const names = res.map(i => i.name)
   console.log(names)
})
Terchila Marian
  • 2,225
  • 3
  • 25
  • 48