I've been trying to get the push() method on a loop to build a structure as follows:
var locations2 = [
['User', position.coords.latitude, position.coords.longitude, 1],
['Bondi Beach', -33.890542, 151.274856, 2],
['Coogee Beach', -33.923036, 151.259052, 3],
['Cronulla Beach', -34.028249, 151.157507, 4],
['Manly Beach', -33.80010128657071, 151.28747820854187, 5],
['Maroubra Beach', -33.950198, 151.259302, 6]
];
This is my code:
var locations = [];
$.ajax({
type: "POST",
url: "/ajax/map.php",
data: "name=test",
dataType: "json",
cache: false,
success: function(data){
$.each(data.points, function(i,item){
array_push = ["test", parseFloat(item.origem_lat), parseFloat(item.origem_lng), i];
locations.push(array_push);
});
}
});
However, the Javascript console.log for locations shows an empty array.
I have tried to use push() in many different ways, but I cannot get the same structure as locations2. The biggest problem here is that I do not know how many arrays are going to be inside the locations array before the loop, so I can't initialize it beforehand.
Any thoughts?
I've been trying to get the push() method on a loop to build a structure as follows:
var locations2 = [
['User', position.coords.latitude, position.coords.longitude, 1],
['Bondi Beach', -33.890542, 151.274856, 2],
['Coogee Beach', -33.923036, 151.259052, 3],
['Cronulla Beach', -34.028249, 151.157507, 4],
['Manly Beach', -33.80010128657071, 151.28747820854187, 5],
['Maroubra Beach', -33.950198, 151.259302, 6]
];
This is my code:
var locations = [];
$.ajax({
type: "POST",
url: "/ajax/map.php",
data: "name=test",
dataType: "json",
cache: false,
success: function(data){
$.each(data.points, function(i,item){
array_push = ["test", parseFloat(item.origem_lat), parseFloat(item.origem_lng), i];
locations.push(array_push);
});
}
});
However, the Javascript console.log for locations shows an empty array.
I have tried to use push() in many different ways, but I cannot get the same structure as locations2. The biggest problem here is that I do not know how many arrays are going to be inside the locations array before the loop, so I can't initialize it beforehand.
Any thoughts?
Share Improve this question edited Jan 9, 2013 at 22:30 Lightness Races in Orbit 385k77 gold badges666 silver badges1.1k bronze badges asked Dec 12, 2012 at 0:52 roddrodd 1931 gold badge2 silver badges11 bronze badges 10-
1
Your syntax looks right -- are you sure
data.points
contains the array you're expecting? – McGarnagle Commented Dec 12, 2012 at 0:54 - 1 Add an error callback to your ajax call. It may be throws an error but you may not know with this code. – Emre Erkan Commented Dec 12, 2012 at 0:56
- You can do a quick dump of your array contents with alert( JSON.stringify(x) ) where x is the array in question. – RonaldBarzell Commented Dec 12, 2012 at 0:56
-
5
Do not forget to put the
console.log
statement in the callback, otherwise it won't be populated yet. – Bergi Commented Dec 12, 2012 at 0:57 - 1 please post your result json to validate! – silly Commented Dec 12, 2012 at 7:59
1 Answer
Reset to default 4I figured out what the problem is. It has nothing to do with the push() method itself. Apparently ajax calls are not executed in sequential order (that's the reason it's asynchronous). I added async: false in the ajax call options and now everything works perfectly.
Thanks everyone for the input.