首页 > 解决方案 > 使用 Open Weather Map 每小时数据

问题描述

Open Weather Map 是一个开源天气提供者,并且有一个 API 可以提供接下来 2 天的每小时天气数据,因此有 48 个数据点。它将数据作为对象返回,并如下所示进行设置。

{
"hourly": [
{
  "dt": 1631725200,
  "temp": 16.75,
  "feels_like": 16.33,
  "pressure": 1014,
  "humidity": 71,
  "dew_point": 11.47,
  "uvi": 0,
  "clouds": 20,
  "visibility": 10000,
  "wind_speed": 3.33,
  "wind_deg": 88,
  "wind_gust": 7.37,
  "weather": [
    {
      "id": 801,
      "main": "Clouds",
      "description": "few clouds",
      "icon": "02d"
    }
  ],
  "pop": 0
},
....
}

我想从每个小时获取温度,并尝试通过循环循环来完成它,但它作为一个字符串返回并在一个长字符串中返回所有 48 个温度。weatherResponse 部分是使用 axios 调用 api 的 const。

let hourlyTemp = [];
  for (let i = 1; i < 48; i++) {
   hourlyTemp += [weatherResponse.data.hourly[i].temp] + " °C " + "<br>"
  }

是否有不同的方法来编写循环,使其以对象或数组的形式返回?或者有没有办法在之后拆分字符串?

非常感谢!

标签: stringfor-loopopenweathermap

解决方案


推荐阅读