如何访问每个和元素features
中的每个数组值?我试过这样:condo
house
attr.features.name,
...但undefined
会在 Web 控制台中弹出。
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' });
}
});
});