Javascript 为什么我的函数得到0个结果?
嘿,我这里有3个函数一起工作。但问题是最后一个被称为doesn的数组并没有遍历它,因为它看到的是一个空数组。有趣的是,当我执行Javascript 为什么我的函数得到0个结果?,javascript,asynchronous,Javascript,Asynchronous,嘿,我这里有3个函数一起工作。但问题是最后一个被称为doesn的数组并没有遍历它,因为它看到的是一个空数组。有趣的是,当我执行console.log()整个对象时,它在该数组中有一个数据,但当我只记录该对象中的数组元素时,它是空的。为什么? 这是一支密码笔: 下面是它的样子: getPlacesInArea: function($scope, types, callback) { for (var key in types) { if (types.hasOwnPrope
console.log()
整个对象时,它在该数组中有一个数据,但当我只记录该对象中的数组元素时,它是空的。为什么?
这是一支密码笔:
下面是它的样子:
getPlacesInArea: function($scope, types, callback) {
for (var key in types) {
if (types.hasOwnProperty(key) && types[key].checked) {
this.getPlace($scope, key);
}
}
callback($scope);
},
所以这里发生的事情就是我将数据从api加载到范围VARABLE
getAttractionsInArea: function($scope, typesValue) {
placesService.getPlacesInArea($scope, typesValue, this.showAttractionMarkers);
},
showAttractionMarkers: function($scope) {
placesService.showPlacesMarkers($scope.map, $scope.attractionsMarkers);
}
这里是调用getPlacesInArea
的位置。
在getAttractionsInArea
中,我还将其作为回调showAttractionMarkers
方法传递
showPlacesMarkers: function(map, markers) {
if (typeof map != 'undefined') {
for (var key in markers) {
if (markers.hasOwnProperty(key)) {
mapService.setMapForMarkers(markers[key].items, map);
}
}
} else {
for (var type in markers) {
if (markers.hasOwnProperty(type)) {
mapService.setMapForMarkers(markers[type].items, null);
}
}
}
},
如果我执行console.log(markers)
我看到markers[type].items有items,那么我们就在这个函数中了。但是如果我执行console.log(markers[type].items)
则数组为空。为什么呢?我怎样才能修好它
这里是console.log(标记)代码>
这是食物对象的例子
food: Object
checked: true
icon: null
items: Array[20]
0: MarkerWithLabel
1: MarkerWithLabel
2: MarkerWithLabel
3: MarkerWithLabel
4: MarkerWithLabel
5: MarkerWithLabel
6: MarkerWithLabel
7: MarkerWithLabel
8: MarkerWithLabel
9: MarkerWithLabel
10: MarkerWithLabel
11: MarkerWithLabel
12: MarkerWithLabel
13: MarkerWithLabel
14: MarkerWithLabel
15: MarkerWithLabel
16: MarkerWithLabel
17: MarkerWithLabel
18: MarkerWithLabel
19: MarkerWithLabel
length: 20
__proto__: Array[0]
__proto__: Object
var$scope,
objectByString=函数(o,s){
s=s.替换(/\[(\w+)\]/g,.$1');
s=s.替换(/^\./,'');
var a=s.分割('.');
对于(变量i=0,n=a.length;ifood: Object
checked: true
icon: null
items: Array[20]
0: MarkerWithLabel
1: MarkerWithLabel
2: MarkerWithLabel
3: MarkerWithLabel
4: MarkerWithLabel
5: MarkerWithLabel
6: MarkerWithLabel
7: MarkerWithLabel
8: MarkerWithLabel
9: MarkerWithLabel
10: MarkerWithLabel
11: MarkerWithLabel
12: MarkerWithLabel
13: MarkerWithLabel
14: MarkerWithLabel
15: MarkerWithLabel
16: MarkerWithLabel
17: MarkerWithLabel
18: MarkerWithLabel
19: MarkerWithLabel
length: 20
__proto__: Array[0]
__proto__: Object
function dataLoaded () {
if (markers.drinks.items.length > 1) {
showPlacesMarkers();
} else {
dataLoaded();
}
}
service.nearbySearch(request, function (results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var markers = [],
amount = objectByString($scope.searchData, propertyPath + '.amount');
for (var i = 0; i < results.length; i++) {
var place = results[i];
var typeIcon = null;
if (localStorageService.checkPropertyExistence($scope, [markersPath, key, 'icon'])) {
typeIcon = objectByString($scope, markersPath + '.' + key + '.icon');
}
var markerIcon = (typeIcon !== null ? typeIcon : (icon !== null ? icon : place.icon)),
marker = mapService.addMarker(place.geometry.location, null, markerIcon);
mapService.addInfoBubble(marker, place.name, place.vicinity.substr(0, 70), markerIcon, $scope.map, key.replace(/_/g, ' '));
markers.push(marker);
if (localStorageService.checkPropertyExistence($scope, [markersPath, key, 'items'])) {
$scope[markersPath][key]['items'] = markers;
}
}
localStorageService.addToProperty($scope.searchData, propertyPath + '.amount', amount + markers.length);
localStorageService.save($scope.searchData, "searchData");
if (checked)) {
mapService.setMapForMarkers(markers, $scope.map);
}
}
});