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 )


    })
  }
2
  • Where is "narrative" coming from? isn't it "dayOrNight"? Commented Aug 9, 2019 at 21:12
  • sorry l updated Commented Aug 9, 2019 at 21:16

2 Answers 2

3

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 
}
Sign up to request clarification or add additional context in comments.

Comments

2

You should try with:

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

Since it is an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.