简体   繁体   English

如何使用 OpenWeatherMap API 返回每天的最低和最高温度

[英]How to return min and max temperature for each day with OpenWeatherMap API

I am using a API same as this one: https://samples.openweathermap.org/data/2.5/forecast/daily?q=Belgrade,uk&appid=439d4b804bc8187953eb36d2a8c26a02我正在使用与此相同的 API: https://samples.openweathermap.org/data/2.5/forecast/daily?q=Belgrade,uk&appid=439d4b804bc8187953eb366

I am trying to fetch data but I don't know how to filter data response for each day individually.我正在尝试获取数据,但我不知道如何分别过滤每一天的数据响应。

This is what I am trying to do:这就是我想要做的:

fetch(
  "https://api.openweathermap.org/data/2.5/forecast/daily?q=Belgrade&&units=metric&cnt=7&appid=...."
)
  .then((response) => response.json())
  .then((response) => {
    for (var i=0; i<response.length; i++)
    for (var temp in response[i]) {
        console.log("Min temp: "+response[i][temp].min);
        console.log("Max temp: "+response[i][temp].max);
    }
})

This is what I want to return:这就是我想要返回的:

Day: Today
Min_temp: 16
Max_temp: 20

Day: Tomorrow
Min_temp: 15
Max_temp: 21

I'm assuming i is day offset, and that response[i] is an array of temperatures (hourly, or whatever) that are Numbers (not strings).我假设i是日期偏移量,并且response[i]是一个温度数组(每小时或其他),它们是数字(不是字符串)。 Then:然后:

"min: "+
response[i].reduce((min, num)=>((num<min)?(num):(min)))+
"<br>"+
"max: "+
response[i].reduce((max, num)=>((num>max)?(num):(max)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM