I have some javascript array that I append within a function and call after the function has run (the db information is a call to CouchDB):
$(document).ready(function() {
my_array = [];
view_name = db_name+'/farms_by_name'
$db.view(view_name, {
success: function(data) {
console.log(data)
for (i in data.rows) {
console.log(data.rows[i]);
plot_name.push(data.rows[i].value.name);
my_array.push("0");
console.log(my_array)
console.log(my_array.length)
}
},
error: function(e) {
alert('Error loading from database: ' + e);
}
});
console.log(my_array);
console.log(my_array.length);
});
Using firebug, I see that within the function the array is appended correctly, for e.g. a single element the console.log returns
my_array = ["0"]
my_array.length = 1
but the console.log after the function (which appears first in the firebug log) shows:
my_array = [ ]
my_array.length = 1
and my_array when inspected in the log reveals:
[ ]
0 "0"
Can anyone explain this behaviour to me and explain how I should correctly append the array such that I can call the length and loop through it correctly outside of the db success function?
Thanks!