Create variable with an object property - Angular

Asked

Viewed 396 times

1

I am trying to create a variable to set one of the properties of an object obtained by the get method.

When I give console no subscribe I recover the value of the array, but I’m having difficulty (being beginner) to set only one of the proprierades of the objects of this array.

Component:

this.mainService.getGraph()
    .subscribe(res => {
      console.log(res) 
      this.name = res[''].map(res => res.name)
      console.log(this.name)

Console.log:

inserir a descrição da imagem aqui

  • Put a number instead of ' ' ' here: res['']. map . If you solve let me know that I will put as a response.

  • already put, as Undefined also.

  • In your question it was not very clear. You want the this.name receive only one of the names present in the array? Or receive an array like all the names in the original array?

  • an array with all the names of the original array. the goal is to take these values to create a chart.

1 answer

1


To create a new array with the names of the original array, just use the map method straight into the array:

this.name = res.map(it => it.name)
  • I had already solved, but thanks for the help! That’s exactly what I did :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.