2015-11-04 237 views
-1

我有一個Javascript數據對象與我需要顯示的所有數據(我從API獲取數據)。Javascript - 訪問對象屬性

我不明白爲什麼我不能訪問它的屬性。這是我的數據(從執行console.log):

[Object, Object, Object, Object] 
0: Object 
distancia: "2.69" 
id_servicio: "1" 
latitud: "42.2261474" 
longitud: "-8.7604453" 
service_data: Array[1] 

1: Object 
distancia: "0.65" 
id_servicio: "2" 
latitud: "42.2272562" 
longitud: "-8.7340278" 
service_data: Array[1] 

2: Object 
distancia: "2.36" 
id_servicio: "3" 
latitud: "42.2338588" 
longitud: "-8.7030846" 
service_data: Array[1] 

3: Object 
distancia: "4.42" 
id_servicio: "4" 
latitud: "42.2154313" 
longitud: "-8.6754155" 
service_data: Array[1] 

現在我有一個循環accesing的屬性,但它拋出一個錯誤。我已經檢查每一個組合,但沒有任何工程:

for (i = 0; i<=datos.length; i++) { 
     var servicio = datos[i]; 
     var nLatitud = servicio.latitud; 
     var nLongitud = servicio.longitud; 
     console.log(servicio); 
     var auxll = new google.maps.LatLng(nLatitud,nLongitud); 
     var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map 
     }); 
     marker.setMap($scope.map); 
    } 

而且,這是我得到的錯誤:

TypeError: Cannot read property 'latitud' of undefined 
    at generarMapa (myDashboardController.js:134) 
    at myDashboardController.js:94 
    at ionic.bundle.js:18899 
    at processQueue (ionic.bundle.js:23394) 
    at ionic.bundle.js:23410 
    at Scope.parent.$get.Scope.$eval (ionic.bundle.js:24673) 
    at Scope.parent.$get.Scope.$digest (ionic.bundle.js:24484) 
    at Scope.parent.$get.Scope.$apply (ionic.bundle.js:24778) 
    at done (ionic.bundle.js:19191) 
    at completeRequest (ionic.bundle.js:19363) 
+0

經典差一錯誤... – deceze

+0

嗯,有時候錯誤描述混淆你的頭腦,遺憾的困惑:_ –

回答

3

更改for語句。它的訪問之外出數組由<=

for (i = 0; i<=datos.length; i++) { 

for (var i = 0; i < datos.length; i++) { 
+1

我只是一個白癡。哦,上帝,離子框架的錯誤並不準確......我專注於訪問對象。上帝,非常感謝! –