【发布时间】:2020-05-21 19:25:08
【问题描述】:
我使用 OpenWeatherMap API 创建了一个简单的天气应用程序。架构如下:
{
"coord": {
"lon": -122.08,
"lat": 37.39
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.55,
"feels_like": 281.86,
"temp_min": 280.37,
"temp_max": 284.26,
"pressure": 1023,
"humidity": 100
}...
我正在尝试访问包含数组的天气键中的值。这是我尝试过的:
let iconImage = '';
data.weather.map((description, icon) => {
this.description.textContent += description;
iconImage += icon;
});
this.icon.setAttribute('src', data.weather.icon);
description 和 icon 是变量,它们指向我的 html 代码中的 li 元素,我从 API 获得的数据应该进入这些元素。
我该如何以正确的方式做到这一点??
【问题讨论】:
标签: javascript arrays json api fetch