【发布时间】:2021-09-05 19:20:54
【问题描述】:
我正在尝试创建一个 for 循环来获取每天的最低和最高温度并将其输出。我正在使用开放天气 API。我附上了一张数据在调用时的样子。请随时提出任何问题。我不知道要澄清什么类型的事情。这是我的javascript。
fetch('https://api.openweathermap.org/data/2.5/onecall?lat=43.6121&lon=-116.3915&exclude=hourly,minutely&units=imperial&APPID=98cbc04c44e9fc2905a70710f2643e4e')
.then((response) => response.json())
.then((jsObject) => {
console.log(jsObject);
let temp = jsObject.current.temp;
let minTemp = jsObject.daily[0].temp.min;
let maxTemp = jsObject.daily[0].temp.max;
document.getElementById('condition').textContent = jsObject.current.weather[0].main;
document.getElementById('temp-min').textContent = Math.round(minTemp);
document.getElementById('temp-max').textContent = Math.round(maxTemp);
document.getElementById('temp-current').textContent = Math.round(temp);
var weekday = new Array(7);
weekday[0] = "Sun";
weekday[1] = "Mon";
weekday[2] = "Tue";
weekday[3] = "Wed";
weekday[4] = "Thu";
weekday[5] = "Fri";
weekday[6] = "Sat";
var data = jsObject.daily;
var dayOfWeek = document.getElementsByClassName("forecast-day");
var weatherIcon = document.getElementsByClassName("weatherIcon");
var tempMax = document.getElementsByClassName("forecast-max");
var tempMin = document.getElementsByClassName("forecast-min");
for (var i = 0; i < data.length; i++) {
var d = new Date(data[i]);
dayOfWeek[i].textContent = weekday[d.getDay()];
const imagesrc = 'https://openweathermap.org/img/w/' + data[i].weather[i].icon + '.png';
const description = data[i].weather[i].description;
weatherIcon[i].setAttribute('src', imagesrc);
weatherIcon[i].setAttribute('alt', description);
tempMax[i].innerHTML = Math.round(data[i].temp.max) + " °F";
tempMin[i].innerHTML = Math.round(data[i].temp.min) + " °F";
}
【问题讨论】:
-
您可以解释代码的当前行为以及您的期望。还包括minimal reproducible example。
标签: javascript arrays api for-loop foreach