2015-11-06 130 views
0

我有這個json結構,出於某種原因,我無法訪問其他屬性。我只能通過typeproperties等主要屬性「訪問」該層級。但我無法訪問iconiconURL,title。數據在那裏,但我可以單獨訪問。無法訪問所有json屬性

如果我使用console.log(markerRE[i].properties),它會顯示所有屬性,但如果我只想要標題,則控制檯會觸發can't read property properties of undefined

var markerRE = [{}]; //JSON ARRAY 

$.ajax({ 
    url: 'consulta4.php', 
    data: {}, 
    method: 'POST', 
    dataType: 'json', 
    success: function (data) { 
     for (var i = 0; i < Object.keys(data).length; i++) { 
      markerRE.push({ 
        "type": "Feature", 
        "geometry": { 
         "type": "Point", 
         "coordinates": [data[i].longitude, data[i].latitude] 
        }, 

        "properties": { 
         "title": "Emergencia: " + data[i].priority, 
         "id": data[i].id_emergency, 
         //"descripcion":"<button class='RE'>oisdjos</button>", Forma alternativa de añadir html 
         "icon": { 
          "iconUrl": data[i].imagen, 
          "iconSize": [50, 50], // size of the icon 
          "iconAnchor": [25, 50], // point of the icon which will correspond to marker's location 
          "popupAnchor": [0, -25], // point from which the popup should open relative to the iconAnchor 
          "className": "dot" 
         } 
        } 
       } 


      ); 
      console.log(markerRE[i].properties.title); 
     } 
    } 
}); 
+0

你爲什麼要迭代Object.keys? – Rayon

+0

即時迭代它,因爲我想存儲來自ajax的所有數據 –

+0

['Object.keys'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys )將返回對象的屬性。您可能需要一個for循環。需要查看'數據' – Rayon

回答

0

它沒有很大的意義對我來說你在上面做什麼,只要通過按鍵在您的數據的數量的循環。但我認爲你的問題之一是你正在實例化markerRE,其中已經有一個空對象。而不是僅僅把它變成一個空的數組[]。所以console.log(markerRE [0] .properties.title);將是無效的,因爲markerRE [0]是一個空對象

1

你嘗試宣告markerRE作爲

var markerRE = []; //without {} 

我想在我自己的方式BT不知道和我不知道爲什麼,但使用{}使計數器從1開始不是0.