0

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
    }
];
3
  • Does the console show an error? Commented May 14, 2014 at 21:40
  • 1
    Should it be var places = []; instead of var places[];? Commented May 14, 2014 at 21:41
  • In $.each you're pushing arrays, in your for loop you're pushing objects. Which do you want? Commented May 14, 2014 at 21:41

2 Answers 2

1

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.

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

Comments

1

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.

Comments

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.