1

How can I access each value of features array within every condo and house element? I tried it like so:

attr.features.name,

... but undefined pops up in the web console.

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 ...}, {...}]})

Code:

    $.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 Answer 1

1

You have an object with Two Arrays. You should access each array separately

$(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'
    });
});​

and same for markers.houses

Sign up to request clarification or add additional context in comments.

2 Comments

But wouldn't the elem.features access both id and name of features array? I want to only access the name.
You have an array of features in your JSON. Which name would you like to access?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.