我有這個json結構,出於某種原因,我無法訪問其他屬性。我只能通過type
,properties
等主要屬性「訪問」該層級。但我無法訪問icon
,iconURL
,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);
}
}
});
你爲什麼要迭代Object.keys? – Rayon
即時迭代它,因爲我想存儲來自ajax的所有數據 –
['Object.keys'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys )將返回對象的屬性。您可能需要一個for循環。需要查看'數據' – Rayon