2012-06-12 2 views
1

condohouse 요소 내에 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' }); 
      } 
     }); 
    }); 

답변

1

당신은 두 배열과 객체가 있습니다. 당신은 markers.houses

+0

에 대한 별도

$(markers.condos).each(function(index, elem) { $('#map').gmap('addMarker', { 'features': elem.features, 'position': new google.maps.LatLng(parseFloat(elem.lat), parseFloat(elem.lng)), 'bounds': true, 'icon': 'condo.png' }); });​ 

와 같은 각 배열에 액세스해야하지만 것이 아니라'elem.features' 액세스'id'과'features' 배열의'name' 모두? 나는'name'에 접근하고 싶다. – Bob

+0

JSON에 다양한 기능이 있습니다. 어느 이름에 액세스 하시겠습니까? –

관련 문제