Search code examples
jsonangularionic5

Struggle accessing array in json object - ANGULAR


i would like to access the values assigned to the property opzioni in this json i get from my api, but i have some issues doing it.

[
 { 
   "isActive": "yes",
   "_id": "60e04e583a5c9f0004ac1524",
   "titolo": "Test123",
   "sottotitolo": "bla bla bla bla",
   "descrizione": "lorem ipsum dolor sit amet",
   "tipo": "pal",
   "opzioni": [
                {
                  "nome": "Name 1"
                },
                {
                  "nome": "Name 2"
                 },
                 {
                  "nome": "Name 3"
                 }
              ],
    "__v": 0
    }
]

Solution

  • I see the root object is an array. You can access specific attributes within an array using the map function, for example:

    const data = [ { "isActive": "yes", "_id": "60e04e583a5c9f0004ac1524", "titolo": "Test123", "sottotitolo": "bla bla bla bla", "descrizione": "lorem ipsum dolor sit amet", "tipo": "pal", "opzioni": [ { "nome": "Name 1" }, { "nome": "Name 2" }, { "nome": "Name 3" } ], "__v": 0 } ]
    
    const opzioniArray = data.map(i => i.opzioni);
    

    This will remap the array to be a list of all opzioni values.