我有一个名为问题的空数组,我使用$.get
方法将对象添加到数组中。在$.get
内,数组充满了对象并且工作正常。但是一旦在$.get
之外,数组会再次变空,因为array.length
会返回0
。
var questions=[];
var input;
$(document).ready(function(){
$.get('songs.html', function(list){
var cat = $(list).find('li');
var dog = [];
var count = 0;
var n;
cat.each(function(i, li){
dog[count] = $(li).text();
count = count + 1;
if(count == 3){
n = Math.floor(Math.random()*4);
questions.push({"song":"","choice1":dog[0],"choice2":dog[1],"choice3":dog[2],"choice4":dog[3], "answer":dog[n]});
count = 0;
}
});
console.log(questions.length);//array has length
});
console.log(question.length);