1
condo
및 house
요소 내에 features
배열의 각 값에 어떻게 액세스 할 수 있습니까? 그래서 같은 그것을 시도 :JSON에서 배열 내의 값에 액세스하는 방법은 무엇입니까?
attr.features.name,
...하지만 undefined
는 웹 콘솔에 나타납니다.
JSON :
({condos:[{Address:'123 Fake St.', Lat:'56.645654', Lng:'23.534546', features:[{id:'1', name:'Swimming Pool'},{id:'2', name:'BBQ'},{..etc..}]},{... another condo ...},{...}],houses:[{Address:'1 Main Ave.', Lat:'34.765766', Lng:'27.8786674', features:[{...},{...}]},{... another house ...}, {...}]})
코드 :
$.each(markers, function(type, elements) {
$.each(elements, function(index, attr){
// if it's a house - make house marker. if it's a condo - make condo marker.
if (type == 'houses') {
$('#map').gmap('addMarker', { 'features': attr.features.name, 'position': new google.maps.LatLng(parseFloat(attr.lat), parseFloat(attr.lng)), 'bounds':true, 'icon':'house.png' });
}
else if (type == 'condos') {
$('#map').gmap('addMarker', { 'features': attr.features.name, 'position': new google.maps.LatLng(parseFloat(attr.lat), parseFloat(attr.lng)), 'bounds':true, 'icon':'condo.png' });
}
});
});
에 대한 별도
와 같은 각 배열에 액세스해야하지만 것이 아니라'elem.features' 액세스'id'과'features' 배열의'name' 모두? 나는'name'에 접근하고 싶다. – Bob
JSON에 다양한 기능이 있습니다. 어느 이름에 액세스 하시겠습니까? –