0

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 )


    })
  }
  • Where is "narrative" coming from? isn't it "dayOrNight"? – Albondi yesterday
  • sorry l updated – Ali Ghassan yesterday
2

You should try with:

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

Since it is an array.

2

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 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.