簡體   English   中英

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

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

我正在使用與此相同的 API: https://samples.openweathermap.org/data/2.5/forecast/daily?q=Belgrade,uk&appid=439d4b804bc8187953eb366

我正在嘗試獲取數據,但我不知道如何分別過濾每一天的數據響應。

這就是我想要做的:

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);
    }
})

這就是我想要返回的:

Day: Today
Min_temp: 16
Max_temp: 20

Day: Tomorrow
Min_temp: 15
Max_temp: 21

我假設i是日期偏移量,並且response[i]是一個溫度數組(每小時或其他),它們是數字(不是字符串)。 然后:

"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