How do I assign data from a weather api object to individual html elements using javascript?

I’m currently working on building out my first JS weather app, using the Openweathermap API and vanilla javascript. I’m attempting to set it up such that it provides a basic 5 day forecast when a user enters their city into the search field. The API returns an object that contains an array, which contains 5 objects corresponding to each day (one such object included in the code below). I then want to use that data to set the appropriate information to display within the HTML (an example of one of the HTML cards is also provided below).

How do I iterate over this array to apply the correct pieces of data to their corresponding html elements? Specifically I need to grab the current temp, the date, the weather description, and the humidity and apply it to the respective html element.

One object from array of data

 [
  {
    "dt": 1614632400,
    "main": {
      "temp": 56.64,
      "feels_like": 48.09,
      "temp_min": 56.64,
      "temp_max": 56.95,
      "pressure": 1025,
      "sea_level": 1025,
      "grnd_level": 953,
      "humidity": 12,
      "temp_kf": -0.17
    },
    "weather": [
      {
        "id": 800,
        "main": "Clear",
        "description": "clear sky",
        "icon": "01d"
      }
    ],
    "clouds": {
      "all": 1
    },
    "wind": {
      "speed": 4.36,
      "deg": 71
    },
    "visibility": 10000,
    "pop": 0,
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2021-03-01 21:00:00"
  },
]

One HTML card

   <div class="forecast-card">
     <h2 class="date">Saturday</h2>
     <h2 class="current-temp">57</h2>
     <img src="./icons/unknown.png" alt="weather icons" class="icon">
     <p class="conditions">Clear Sky</p>
     <h3 class="high-temp">
      High
     </h3>
     <h3 class="low-temp">
      Low
     </h3>
     <p class="humidity">80%</p>
    </div>

5 thoughts on “How do I assign data from a weather api object to individual html elements using javascript?”

Leave a Comment