2014-06-05 97 views
0

我有一個返回2個數組的javascript函數。每個數組元素都是一個數組,每個數組都包含一個coursject對象。爲什麼不是我的數組被填充?

我知道這很複雜,但我的團隊中的其他人員負責從課程中生成可能的時間表,他們希望我能夠像這樣回到他們的課程表中。

我已經對我有什麼做了一些測試,出於某種原因,數組的第二級沒有被填充或定義。就像如果我把一個可選類作爲我的輸入(我們有一個UI),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); 
} 
+2

Acoustic77,你能possbily提供您所看到的輸出的例子嗎?我嘲笑了一些數據,你的代碼當然似乎正在生成一個數組Array Array:[[{「name」:「class1」,「catalog_num」:「course1」},{「name」:「class2」,「catalog_num」 : 「course1」}],[{ 「名稱」: 「等級2」, 「catalog_num」: 「course2」}],[{ 「名稱」: 「CLASS3」, 「catalog_num」: 「course3」}],[{」名稱「:」class4「,」catalog_num「:」course4「}]] – IrishGeek82

+2

代碼看起來不錯。沒有更多信息,我們可以做的不多。我建議你創建一個http://jsfiddle.net/來重現問題。 –

+0

這裏是一些UI的JSfiddle和上面的完整代碼http://jsfiddle.net/9KDVX/1/(代碼來創建可能的時間表併發送到完整的日曆不包括在內)。我不知道如何格式化我的輸出的評論,但我添加了一個「console.log」行的JavaScript。我一直在測試一個名爲EECS 214-0的可選類。如果你把這些文件放在一個文件夾中,然後用EECS 214-0進行測試,你會看到數組中第一個元素對象的「未定義」 – Acoustic77

回答

2

Acoustic77,

下面是修改queryCourseData方法。

我使用async:false將您的$ .getJSON轉換爲$ .ajax(這可能是也可能不是我的問題,所以我鼓勵您嘗試將其設置爲true並測試它是否適用於您結束或不)。

然後我注意到你的startTime和endTime格式是{小時:#,分鐘:#},而item.start_time和item.end_time是24小時格式的時間字符串。我寫了一些代碼將前一種格式轉換爲24小時格式(我確信有比這更好的做法)。

我也是後來,在我最初的回答後,注意到你正在將myArray設置爲[]你的內循環的每一步,你正在構建ReqcourseArray和OptcourseArrays。將var var myArray = []移到內部是我的最終修復。

我離開了我的console.log,以便您可以看到結果。是

function queryCourseData(startTime, endTime, optCourses, reqCourses, numOptCourses, numReqCourses) 
{ 

var numClasses = optCourses.length; 
var OptclassList = []; 
var i = 0; 
for(var m = 0; m < numClasses; m++) 
{ 
    //[email protected], 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) 
        { 
         //[email protected], 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 
                     }; 

                 //[email protected] 
                 //Now Pushing Entries Into Array 
                 OptclassList.push(coursject); 
                 i++; 
                } 
               } 
              }   
             }); 
        } 
      }); 

}  

var OptcourseArray = [];  
for(var j = 0; j < numOptCourses; j++) 
{ 
    var catNum = optCourses[j].courseNumber; 
    //[email protected] 
    //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++) 
{ 
    //[email protected], 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) 
         { 

         //[email protected], 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 
                     }; 
                 //[email protected] 
                 //Now Pushing Entries Into Array 
                 ReqclassList.push(coursject); 
                 g++; 
                } 
               } 
              }   
             }); 


         } 
      }); 
} 

var ReqcourseArray = [];  
for(var j = 0; j < numReqCourses; j++) 
{ 
    var catNum = reqCourses[j].courseNumber; 
    //[email protected] 
    //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]; 

} 

我的測試結果如下:

測試用例:

2 courses 
EECS 214-0 (optional) 
EECS 223-0 (required) 

結果:

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-- 

測試用例:

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". 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-- 

請讓我知道如果這有助於:)

+0

非常感謝你!這解決了它的完美。我無法感謝你,所有的改變對我來說都是有意義的,我真的很感激你花時間去發現那些錯誤。 – Acoustic77

相關問題