Javascript 为什么不是';我的数组没有被填充吗?
我有一个返回2个数组的javascript函数。每个数组元素都是一个数组,每个数组都包含一个CoursObject对象 我知道这很复杂,但我团队中的其他人负责从课程中生成可能的时间表,他们希望我像那样返回给他们 我已经对我所拥有的运行了一些测试,出于某种原因,第二级数组没有得到填充或定义。就像我将一个可选类作为输入一样(我们有一个UI),Javascript 为什么不是';我的数组没有被填充吗?,javascript,arrays,Javascript,Arrays,我有一个返回2个数组的javascript函数。每个数组元素都是一个数组,每个数组都包含一个CoursObject对象 我知道这很复杂,但我团队中的其他人负责从课程中生成可能的时间表,他们希望我像那样返回给他们 我已经对我所拥有的运行了一些测试,出于某种原因,第二级数组没有得到填充或定义。就像我将一个可选类作为输入一样(我们有一个UI),OptcourseArray包含未定义的元素 下面是创建OptcourseArray的代码。它是否能够正确地生成对象数组?我想我一定是把事情弄糟了 对于代码上下
OptcourseArray
包含未定义的元素
下面是创建OptcourseArray
的代码。它是否能够正确地生成对象数组?我想我一定是把事情弄糟了
对于代码上下文:
numOptCourses
是可选课程的数量。optCourses
是课程对象的数组。courseNumber
是该课程的课程号,与catalog_num
一样。这使得具有多个部分的类进入同一个数组
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);
}
var OptcourseArray=[];
var-catNum=0;
对于(var j=0;j
声学77
下面是修改后的QueryCourta方法
我使用async:false将$.getJSON转换为$.ajax(这可能是我的问题,也可能不是,所以我鼓励您尝试将其设置为true,并测试它是否在您的端工作)
然后我注意到开始时间和结束时间的格式为{hour:#,minute:#},而item.start#u时间和item.end#u时间是24小时格式的时间字符串。我写了一些代码将前一种格式转换成24小时格式(我确信有一种比我更优雅的方式)
在我最初的回答之后,我还注意到,在构建ReqcourseArray和OptCourseArray的内部for循环的每一步中,您都将myArray设置为[]。将var myArray=[]移动到内部for之外是我最后的解决方案
我离开了控制台。日志已经准备好了,你可以看到结果
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];
}
结果:
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--
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--
测试用例:
5 courses:
EECS 214-0 (optional)
EECS 223-0 (required)
EECS 110-0 (required)
EECS 213-0 (required)
EECS 203-0 (optional)
结果:
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--
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--
请让我知道这是否有帮助:)声学77,您能提供一个您看到的输出示例吗?我模拟了一些数据,您的代码似乎确实生成了一个数组:[{“名称”:“class1”,“catalog_num”:“course1”},{“名称”:“class2”,“catalog_num”:“course1”}],{“名称”:“class2”,“catalog_num”:“course2”}],{“名称”:“class3”,“catalog_num”:“course3”}],{“名称”:“class4”,“catalog_num”:“course4”}]代码看起来不错。没有更多的信息,我们无能为力。我建议您创建一个复制问题的JSF,这里是一些UI和上面的完整代码(不包括创建可能的日程安排和发送到完整日历的代码)。我不知道如何格式化我的注释输出,但我在javascript中添加了一行“console.log”。我一直在使用一个名为EECS 214-0的可选类进行测试,如果您将这些文件放在一个文件夹中,并使用EECS 214-0进行测试,您将看到我为arrayupdated JSFIDLE中的第一个元素对象得到的“未定义”:看起来您的代码依赖于OptclassList,它是在您的第一个$.getJSON成功的基础上构建的。由于该操作是异步的,因此在生成OptclassList之前会调用需要OptclassList的代码。这是我目前的想法。我正在修改代码。非常感谢!这很好地解决了问题。我非常感谢你,你所有的改变对我来说都很有意义,我真的很感谢你花时间去发现那些错误。