<分区>
出于某种原因,下面的代码会产生意外行为。
console.log(chartIntervals.length);
显示“0”而不是预期的“10”。
console.log(chartIntervals);
显示一个包含 10 个元素、长度为 10 的数组。
console.log(chartIntervals[3]);
显示“未定义”。
我是 JavaScript 的新手。这是 angularJS Controller 的一部分。 response.data.count 是在另一个服务文件上处理的 django API 响应的整数部分。
如有任何帮助,我们将不胜感激。
var getPriceChartCounts = function(filter) {
var savedMinPrice = filter.priceMin;
var savedMaxPrice = filter.priceMax;
filter.priceMin = 0;
filter.priceMax = 100000;
var chartIntervals = [];
for (var i = 0; i < 10; i++) {
Properties.getByFilter(filter,Map.getMapBounds()).then(function(response){
chartIntervals.push(response.data.count);
});
filter.priceMin = filter.priceMin + 100000;
filter.priceMax = filter.priceMax + 100000;
}
filter.priceMin = savedMinPrice;
filter.priceMax = savedMaxPrice;
console.log(chartIntervals.length);
console.log(chartIntervals);
console.log(chartIntervals[3]);
return chartIntervals;
}