1

Disclaimer: I am new to Angular :-)

I am experimenting with intertwining Angular and a weather api and I am stumped on how to approach displaying nested data in the JSON payload (Namely, the items under the Historical object). I am able to call and display top level items in the payload.

Here's where I am at:

{{ this.weatherData?.current.precip }} <-- this shows the current precipitation successfully
{{ this.weatherData?.historical.maxtemp }} <--- this doesn't display anything

My API url: https://api.weatherstack.com/historical?access_key=000000hidden0000000&historical_date=2019-08-30&query=london

API documentation: https://weatherstack.com/documentation

Screen shot of console: enter image description here

Many thanks for your coaching and patience that comes with a curious newbie.

1
  • Small correction. My query in the console screen shot was "63129". Commented Dec 18, 2019 at 1:51

2 Answers 2

1

The historical data is returned by date, so you would have to do like this to show a single date:

{{ this.weatherData?.historical['2018-08-30'].maxtemp }}

Or in a loop to show all dates:

<div *ngFor="let history of this.weatherData?.historical | keyvalue">
  {{ history.key + ': ' + history.value.maxtemp }}
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

The this.weatherData?.historical is an array with key is as Date. If you are certain that there will only be 1 value in this.weatherData?.historical array, you should be able to access it like below:

this.weatherData?.historical[0].maxtemp

2 Comments

Thank you both. These responses are quite helpful. This may be a product of my code formatting plugin, but my editor insists on formatting the code like this: {{ (this.weatherData?.historical.maxtemp)[0] }} The parentheses may be throwing it off..?
Ope. I disabled the code formatting plugin to allow the code to be saved without the parentheses. Now, I am getting a Cannot read property '0' of undefined. It seems the array syntax is not working out.

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.