Ali Ghassan
Ali Ghassan

Reputation: 1180

Angular get json array inside another array

I have following JSON struncture:

{
  "daypart": [
    {
      "dayOrNight": [
        null,
        "N",
        "D",
        "N",
        "D",
        "N",
        "D",
        "N",
        "D",
        "N",
        "D",
        "N"
      ]
    }
  ]
}

What i want to do is reach keys array inside narrative . But whenever I try to get those keys l get forecast undefined or forecast [object Object]

Code

 async forcast(){
    this.http.get("xxxxxxxxxxxxxxx")

    .subscribe(data => {

      let f = data
      this.dforecast = f["daypart"]["narrative"]


      console.log("forecast "+ this.dforecast )


    })
  }

Upvotes: 0

Views: 153

Answers (2)

wentjun
wentjun

Reputation: 42516

f['daypart'] is an array. Thefore, you will need to reference it by its index.

this.dforecast = f['daypart'][0]['narrative'];

In addition, there is no need to declare forcast() as an async method, as we are dealing with observables, rather than Promise-based responses.

forcast() {
 // rest of your cose 
}

Upvotes: 3

Albondi
Albondi

Reputation: 1151

You should try with:

this.dforecast = f["daypart"][0]["dayOrNight"]

Since it is an array.

Upvotes: 2

Related Questions