Javascript 如何在JSON中访问数组中的数组值?
如何访问每个Javascript 如何在JSON中访问数组中的数组值?,javascript,jquery,json,google-maps,google-maps-api-3,Javascript,Jquery,Json,Google Maps,Google Maps Api 3,如何访问每个公寓和房屋元素中功能数组的每个值? 我这样试过: attr.features.name, 。。。但是未定义的会在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 ...},{...
公寓
和房屋
元素中功能
数组的每个值?
我这样试过:
attr.features.name,
。。。但是未定义的
会在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' });
}
});
});
代码:
({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' });
}
});
});
您有一个具有两个数组的对象。您应该分别访问每个阵列
$(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'
});
});
对于
标记也一样。house
您有一个具有两个数组的对象。您应该分别访问每个阵列
$(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'
});
});
markers.house
也一样,但是elem.features
不会同时访问features
数组的id
和name
?我只想访问名称
。您的JSON中有一系列功能。您希望访问哪个名称?但是元素特性
是否同时访问特性
数组的id
和名称
?我只想访问名称
。您的JSON中有一系列功能。您希望访问哪个名称?