如何获得当天的最低和最高温度,从5天天气预报JS?

问题描述 投票:0回答:3

我正在构建需要显示5天天气预报的网络应用程序。我从https://openweathermap.org/forecast5获取数据我的想法是使用来自数组的8个索引的数据来输入每天的信息。我的英文不好用,这里有截图以便更好地理解 - https://imgur.com/a/4RHjZ5g这是API示例 - https://samples.openweathermap.org/data/2.5/forecast?q=M%C3%BCnchen,DE&appid=b6907d289e10d714a6e88b30761fae22

从屏幕截图中可以看出,索引0从最后3小时循环获得天气数据(12,15,16等)。几乎在每种情况下,我都会获得每天的数据。

我无法理解如何获得每天的最小值和最大值。如果今天的数据是从索引0的15:00开始,那么在此之前我无法获得信息。我可以在15:00到00:00之间获得温度。

第一天,我从API获取此信息:

.list[0].dt_txt
.list[0].main.temp
.list[0].weather[0].description

第二个8,第三个16等...

javascript api weather
3个回答
0
投票

如前所述,使用history API。要获得今天+未来4天慕尼黑的价值,请使用以下内容:

const getStartAndEndDateTimestamps = increment => {
    let startDate = new Date();
    startDate.setDate(startDate.getDate() + increment);
    startDate.setHours(0,0,0,0);
    let endDate = new Date();
    endDate.setDate(endDate.getDate() + increment);
    endDate.setHours(23,59,59,999);
    return {
        start: startDate.valueOf(),
        end: endDate.valueOf()
    };
};

const city = 'München';

[0, 1, 2, 3, 4].forEach(increment => {
    const timestamps = getStartAndEndDateTimestamps(increment);
    const requestUrl = `http://history.openweathermap.org/data/2.5/history/city?q=${encodeURIComponent(city)}&type=hour&start=${timestamps.start}&end=${timestamps.end}`;
    // get & process API data
});

0
投票

也许你可以使用历史版本的API。也许其他选择可能是为历史值创建服务器端cookie或小型数据库。


0
投票

根据给定API的响应

let weather=" assign response got from API to here";

let minArray=[];
let maxArray=[];  
for (let i of weather.list)  
 {  minArray.push(Number( i.main.temp_min)) ; 
    maxArray.push(Number(i.main.temp_max));  }
  console.log("min temp is "+ Math.min(...minArray));
 console.log("max temp is "+ Math.max(...maxArray));
© www.soinside.com 2019 - 2024. All rights reserved.