A javascript array appearing as blank like">[]" in the console, But after exploring the blank array I am getting array and objects in it. When trying to access the objects it throwing undefind massege. How to solve the problem.
$("#select_category").keyup(function(){
var query = $("#select_category").val();
var allcategory = [];
$.get('api/autocomplete/' + query).done(function(data) {
$.each(data, function(data, category) {
allcategory.push(category);
});
var mycategorycollection = JSON.stringify(allcategory);
console.log(mycategorycollection);
//Showing json data
});
console.log(allcategory);
//Showing Blank array.
});
Here I noticed two factors;
When the array is placing in the .get()function's scope, then it delivering perfect data. (Please see the code)
When the array is placing out of the .get() function's scope then it delivering blank array. (Please see the code )