More help, different issue. I want to return allLessonsArray from the inner loops, but for some reason it doesn’t reach outside the loop function. I’ve noted where it works, and where it doesn’t work. How do I return this to the outside?
var allLessonsArray= new Array();
$.each(lessonInTopicSectionArray, function(index, lesson){
var lastAttempt = 0;
url='domain/learn/mod/lesson/report.php?id='+lesson.id+'&action=reportdetail&userid='+userid+'&try='+lastAttempt;
$.get(url, function(data) {
var lessonArray= new Array();
$(data).find('tr.r1.lastrow td.cell.c0')
.each(function(index, content) {
var string=jQuery(content).html()
score=parseInt(string.substr(string.length - 1));
lessonArray[index]=score;
});
allLessonsArray[index]={name:lesson.name, score: lessonArray};
//This works.
alert(allLessonsArray[0].name);
alert(allLessonsArray[0].score[0]);
});
//This doesn't work.
alert(allLessonsArray[0].name);
alert(allLessonsArray[0].score[0]);
});
return allLessonsArray;
$.get()is an AJAX ie. an asynchronous request, so you can’t do that as you want.You can do: