简体   繁体   中英

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

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). Then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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