Question

so I wanna create a pack of objects (more like places), to use in a google maps plugin that I am using. What I need to do is create the array "places" where I need to be able to put all the properties that will be handled with the jscript, this is done through a JSON that I am taking from somewhere else, this is working but filling up the array seems to be mission impossible. Thing is, I cant seem to be able to create the array and it just mess everything up and nothing is displaying.

var places = [];
$.getJSON(url,function(data){
    $.each(data.lugar, function(i,user){
        places.push([user.latt.value, user.lng.value, user.nombre.value, user.direccion.value]);
        alert("Se añadio "+user.nombre);
    });
});

I am trying a do a dummy just to see if it works with a simple FOR first, but it is not working.

var places[];

for(var x= 0; x<10; x++){
    places[x] = {
        autoShow: true,
        lat: 53.79+x,
        lng:-1.5426760000000286+x,
        name: "Somewhere "+x

    }
}

I dont know where I am missing out on something. A nomal call should be:

 var places = [

    {
        canEdit: true,
        lat: 53.798823,
        lng:-1.5426760000000286,

    },
    {
        canEdit: true,
        lat: 53.79,
        lng: -1.59,
        name: "Somewhere",
        street: "Over the rainbow, Up high way",
        userData: 99
    }
];
Was it helpful?

Solution

You're improperly creating your array variable: var places[]; is not the correct way to declare an array variable, the correct syntax should be var places = [];. The Mozilla Developer Network article on Arrays has this and much more information on Arrays and their usage in Javascript.

OTHER TIPS

I'd suggest trying $.map instead (made for this sort of thing), like this:

var places = $.map(data.lugar, function(i,user){
    return [user.latt.value, user.lng.value, user.nombre.value, user.direccion.value];
    // Do not check results on the UI thread here.
});
// check the results in on the UI thread here.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top