我使用weather.gov API做了一个天气预报,当我使用这段代码时,我得到了一个错误。
错误:
Uncaught SyntaxError: Lexical declaration cannot appear in a single-statement context
代码:
$.getJSON("https://api.weather.gov/gridpoints/DTX/49,12/forecast", function(data) {
for (var i = 0; i == 14; i++) {
if(data['properties']['periods'][i]['name'] == 'Tonight')
let ex = data['properties']['periods'][i]
}
});
每次我查到这件事,我都会发现一些不和谐的东西。
发布于 2022-11-04 11:59:57
您可以在循环之外声明变量,这也是更有效的方法。
let ex;
$.getJSON("https://api.weather.gov/gridpoints/DTX/49,12/forecast", function(data) {
for (var i = 0; i == 14; i++) {
if(data['properties']['periods'][i]['name'] == 'Tonight')
ex = data['properties']['periods'][i]
} });
https://stackoverflow.com/questions/71162874
复制相似问题