I've been struggling to add a group of objects from a JSON file into an array. Despite trying to use the push
method, the length of the array remains at 0. I'm puzzled by what could be going wrong. The JSON data is being parsed correctly as I'm able to access the values within the loop.
<script type="text/javascript">
// Initializing my array
var myArray = new Array();
function performSearch(){
var url = "http://myjsonurl...";
var counter = 0;
$.getJSON(url, function(response){
$.each(response.data.people, function() {
// Creating a new person object to add to the array
var p = new person(this.name, this.age);
// Attempted to use push instead of manually incrementing a counter,
// yet the length still stays at 0.
myArray[counter] = p;
counter++;
});
});
// Always alerts 0
alert(myArray.length);
}
...
</script>