I have a javascript function that returns 2 Arrays. Each array element is an array, and each of those arrays contains a coursject object.
I know this is complicated, but the other people on my team are in charge of generating possible schedules from the classes and they want me to return to them like that.
I've run some test on what I have, and for some reason the second level of arrays isn't getting populated or defined. Like if I put one optional class as my input (we have a UI), the OptcourseArray has elements that are undefined.
Below is just the code for creating the OptcourseArray. Does it seem to correctly make an array of arrays of objects? I think I must have messed something up in it.
For code context:
numOptCourses is the number of optional courses.
optCourses is an array of course objects.
courseNumber is the course number of the class, as is catalog_num. This is so that classes with multiple sections go into the same array.
var OptcourseArray = [];
var catNum = 0;
for(var j = 0; j < numOptCourses; j++){
catNum = optCourses[j].courseNumber;
var myArray = [];
for(var h = 0; h < OptclassList.length; h++){
if (OptclassList[h].catalog_num === catNum){
myArray.push(OptclassList[h]);
}
}
OptcourseArray.push(myArray);
}
-
2Acoustic77, Could you possbily provide an example of the output that you are seeing? I mocked up some data and your code certainly seems to be producing an Array of Arrays: [[{"name":"class1","catalog_num":"course1"},{"name":"class2","catalog_num":"course1"}],[{"name":"class2","catalog_num":"course2"}],[{"name":"class3","catalog_num":"course3"}],[{"name":"class4","catalog_num":"course4"}]]IrishGeek82– IrishGeek822014年06月05日 05:44:46 +00:00Commented Jun 5, 2014 at 5:44
-
2Code looks fine. Not much we can do without more information. I suggest you create a jsfiddle.net that reproduces the problem.Felix Kling– Felix Kling2014年06月05日 05:55:43 +00:00Commented Jun 5, 2014 at 5:55
-
Here's a JSfiddle of some of the UI and the full code above jsfiddle.net/9KDVX/1 (code to create possible schedules and send to Full Calendar isn't included). I don't know how to format my output for comments, but I added a "console.log" line to the javascript. I've been testing with just one optional class called EECS 214-0 If you put those files in a folder and test with just EECS 214-0 you'll see the "undefined" that I get for the first element object in the arraysingmotor– singmotor2014年06月05日 06:07:35 +00:00Commented Jun 5, 2014 at 6:07
-
updated jsfiddle: jsfiddle.net/acoustic7/c335tsingmotor– singmotor2014年06月05日 06:20:00 +00:00Commented Jun 5, 2014 at 6:20
-
It looks like your code is reliant upon the OptclassList which is being constructed on success of your first $.getJSON. Since the operation is asynchronous the code that requires OptclassList is being called before the OptclassList is being generated. That's my current take anyway. I'm correcting the code now.IrishGeek82– IrishGeek822014年06月05日 06:29:10 +00:00Commented Jun 5, 2014 at 6:29
1 Answer 1
Acoustic77,
Here is the modified queryCourseData method.
I converted your $.getJSON to $.ajax using async:false (this may or may not have been an issue on my end, so I encourage you to try and set it to true and test whether it works on your end or not).
I then noticed that your startTime and endTime are in the format {hour:#,minute:#} while the item.start_time and item.end_time are time strings in 24 hour format. I wrote some code to convert the former form into 24 hour format ( I am certain there is a more elegant way of doing this than I did).
I also later, after my initial answer, noticed that you were setting myArray to [] every step of your inner for loops where you are constructing the ReqcourseArray and OptcourseArrays. Moving var myArray=[] outside of the inner for was my final fix.
I left my console.log's in place so you could see the results.
function queryCourseData(startTime, endTime, optCourses, reqCourses, numOptCourses, numReqCourses)
{
var numClasses = optCourses.length;
var OptclassList = [];
var i = 0;
for(var m = 0; m < numClasses; m++)
{
//IrishGeek82@SO, 2014年06月05日
//Your ajax calls were taking too long to call so the code that needed them was
//getting called before the data was ready.
//This could be a problem on my end so you can always set async to true and test from your end.
$.ajax({url:"http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject="+optCourses[m].subject,
async: false,
dataType: 'json',
success:function(result)
{
//IrishGeek82@SO, 2014年06月05日
//Your start and end times are objects of the format
//{hour:x, minute:x}
//While your item.start_time and item.end_time are 24 hour time strings.
//I am sure there is a more elgant way to do this but here is a dirty conversion
//from one to the other.
var sTime = (startTime.hour<10?"0"+startTime.hour:startTime.hour) + ":" + startTime.minute+"00";
var eTime = (endTime.hour<10?"0"+endTime.hour:endTime.hour) + ":" + endTime.minute+"00";
$(result).each(function (index, item)
{
if (item.start_time > sTime)
{
if (item.end_time < eTime)
{
if (item.catalog_num == optCourses[m].courseNumber)
{
var coursject = {
title: item.title,
professor: item.instructor.name,
catalog_num: item.catalog_num,
section: item.section,
subject: item.subject,
meeting_days: item.meeting_days,
start_time: item.start_time,
end_time: item.start_time
};
//IrishGeek82@SO
//Now Pushing Entries Into Array
OptclassList.push(coursject);
i++;
}
}
}
});
}
});
}
var OptcourseArray = [];
for(var j = 0; j < numOptCourses; j++)
{
var catNum = optCourses[j].courseNumber;
//IrishGeek82@SO
//You were resetting your myArray every time you in the loop below.
//Subsequently, only the last entry would every get added and you were
//getting empty arrays.
var myArray = [];
for(var h = 0; h<OptclassList.length; h++)
{
if (OptclassList[h].catalog_num == catNum)
{
myArray.push(OptclassList[h]);
}
}
OptcourseArray.push(myArray);
}
console.log("--OPT--");
console.log(JSON.stringify(OptcourseArray));
console.log("--OPT--");
var ReqclassList = [];
var g = 0;
for(var n = 0; n < reqCourses.length; n++)
{
//IrishGeek82@SO, 2014年06月05日
//Your ajax calls were taking too long to call so the code that needed them was
//getting called before the data was ready.
//This could be a problem on my end so you can always set async to true and test from your end.
$.ajax({url:"http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject="+reqCourses[n].subject,
async: false,
dataType: 'json',
success: function(result)
{
//IrishGeek82@SO, 2014年06月05日
//Your start and end times are objects of the format
//{hour:x, minute:x}
//While your item.start_time and item.end_time are 24 hour time strings.
//I am sure there is a more elgant way to do this but here is a dirty conversion
//from one to the other.
var sTime = (startTime.hour<10?"0"+startTime.hour:startTime.hour) + ":" + startTime.minute+"00";
var eTime = (endTime.hour<10?"0"+endTime.hour:endTime.hour) + ":" + endTime.minute+"00";
$(result).each(function (index, item)
{
if (item.start_time > sTime)
{
if (item.end_time < eTime)
{
if ($.trim(item.catalog_num) == $.trim(reqCourses[n].courseNumber))
{
var coursject = {
title: item.title,
professor: item.instructor.name,
catalog_num: item.catalog_num,
section: item.section,
subject: item.subject,
meeting_days: item.meeting_days,
start_time: item.start_time,
end_time: item.start_time
};
//IrishGeek82@SO
//Now Pushing Entries Into Array
ReqclassList.push(coursject);
g++;
}
}
}
});
}
});
}
var ReqcourseArray = [];
for(var j = 0; j < numReqCourses; j++)
{
var catNum = reqCourses[j].courseNumber;
//IrishGeek82@SO
//You were resetting your myArray every time you in the loop below.
//Subsequently, only the last entry would every get added and you were
//getting empty arrays.
var myArray = [];
for(var h = 0; h < ReqclassList.length; h++)
{
if ($.trim(ReqclassList[h].catalog_num) == $.trim(catNum))
{
myArray.push(ReqclassList[h]);
}
}
ReqcourseArray.push(myArray);
}
console.log("--REQ--");
console.log(JSON.stringify(ReqcourseArray));
console.log("--REQ--");
return [OptcourseArray, ReqcourseArray];
}
The results of my testing are as follows:
Test Case:
2 courses
EECS 214-0 (optional)
EECS 223-0 (required)
Results:
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS".
--OPT--
[[{"title":"Data Structures & Data Management","professor":"Morteza Amir Rahimi","catalog_num":"214-0","section":"21","subject":"EECS","meeting_days":"MoWeFr","start_time":"11:00:00","end_time":"11:00:00"}]]
--OPT--
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
--REQ--
[[{"title":"Fundamentals of Solid State Engineering","professor":"Koray Aydin","catalog_num":"223-0","section":"01","subject":"EECS","meeting_days":"MoTuWeFr","start_time":"09:00:00","end_time":"09:00:00"}]]
--REQ--
Test Case:
5 courses:
EECS 214-0 (optional)
EECS 223-0 (required)
EECS 110-0 (required)
EECS 213-0 (required)
EECS 203-0 (optional)
Results:
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
--OPT--
[[{"title":"Data Structures & Data Management","professor":"Amartya Banerjee","catalog_num":"214-0","section":"20","subject":"EECS","meeting_days":"MoWeFr","start_time":"09:00:00","end_time":"09:00:00"},{"title":"Data Structures & Data Management","professor":"Morteza Amir Rahimi","catalog_num":"214-0","section":"21","subject":"EECS","meeting_days":"MoWeFr","start_time":"11:00:00","end_time":"11:00:00"}],[{"title":"Introduction to Computer Engineering","professor":"Hai Zhou","catalog_num":"203-0","section":"01","subject":"EECS","meeting_days":"MoWeFr","start_time":"11:00:00","end_time":"11:00:00"}]]
--OPT--
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
XHR finished loading: GET "http://vazzak2.ci.northwestern.edu/courses/?term=4540&subject=EECS". jquery.min.js:4
--REQ--
[[{"title":"Fundamentals of Solid State Engineering","professor":"Koray Aydin","catalog_num":"223-0","section":"01","subject":"EECS","meeting_days":"MoTuWeFr","start_time":"09:00:00","end_time":"09:00:00"}],[{"title":"Introduction to Computer Programming","professor":"Aleksandar Kuzmanovic","catalog_num":"110-0","section":"20","subject":"EECS","meeting_days":"MoTuWeFr","start_time":"10:00:00","end_time":"10:00:00"}],[{"title":"Introduction to Computer Systems","professor":"Peter A Dinda","catalog_num":"213-0","section":"20","subject":"EECS","meeting_days":"TuTh","start_time":"14:00:00","end_time":"14:00:00"}]]
--REQ--
Please let me know if this helps :)