0

I am trying to create an associative array for the first time and acces data with keys. My array builds fine but I am having a hard time figuring out how to get to the data. The console.log at the end returns undefined.

placesdata = [];

$.getJSON("php/data.php", function(data) {

    $.each(data.places, function(j,val){

        placesdata.push({

            placename: data.places[j].name,
            placedescription: data.places[j].notes,
            placelong: data.places[j].lon,
            placelat: data.places[j].lat,

        });

    });

    for(var i=0; i< placesdata.length; i++){
        console.log(placesdata[i][0]);
    }

}); 

Thanks!

barrylachapelle
  • 967
  • 4
  • 13
  • 34

1 Answers1

1

You have to reference the pieces of the object within the array, so to print the placename:

for(var i=0; i< placesdata.length; i++){
    console.log(placesdata[i].placename);
}

you would do that for each of the attributes.

caspian
  • 1,804
  • 14
  • 14