将项目推入数组并进行打印会给我一个空数组

时间:2019-06-01 21:28:48

标签: javascript

我不明白我现在在做什么错,我想添加每个项目并将每个项目附加到一个数组中,最后,控制台记录它。

// Loop through each cities in canada and perform API call

var Cities = [];
var AQI = [];
canadian_cities.forEach(function(city){
    var url = `https://api.waqi.info/feed/${city}/?token=${api_key}`

    // call api
    d3.json(url).then(function(response){
        // ignore calls with status:error => no station in that city
        if (response.status == "ok"){
            console.log(response);

            // Create array of Cities, AQI,
            Cities.push(city);
            // console.log(Cities);
        };
    });

});
console.log(Cities);

canadian_cities是一个数组,在加拿大有100个最大的城市。

如果response.status ==“ ok”如何将每个城市推送到Cities数组并在forEach循环结束时打印完整列表?

0 个答案:

没有答案