2017-04-19 23 views
0

我在angularjs中有下面的代碼,但是當我想要我的數組的長度爲0時,它的顯示undefined請檢查下面的代碼,這就是爲什麼我的第二個循環無法運行。 - 在角度如何解決這個問題類型的人幫我請如何獲得angularjs的長度,因爲我的第二個循環沒有運行的長度

 this.current_engineer = response.data; 
       var current_schedules = this.current.schedule; 
       console.log(current_schedules); 
       console.log(current_schedules.length); 
       console.log(current_schedules[0].length); 

      if (angular.isArray(current_schedules)) { 
       for (var j = 0; j < current_schedules.length; j++) { 

       for (var i = 0; i < current_schedules[j].length; i++) //this loop does not run because of length how to get length 
    { 
       console.log(current_schedules[j][i].title); 

       } 
       } 
      } 
///------------Mongodb Database----------------------- 
"schedule" : [ 
     [ 
      { 
       "_id" : ObjectId("58f76aa9be4d311a78f24f78"), 
       "sch_end" : ISODate("2017-04-21T00:00:00.000Z"), 
       "sch_start" : ISODate("2017-04-21T00:00:00.000Z"), 
       "available" : false, 
       "title" : "test3" 
      }, 
      { 
       "_id" : ObjectId("58f76aa9be4d311a78f24f77"), 
       "sch_end" : ISODate("2017-04-22T00:00:00.000Z"), 
       "sch_start" : ISODate("2017-04-22T00:00:00.000Z"), 
       "available" : false, 
       "title" : "test4" 
      }, 
      { 
       "_id" : ObjectId("58f76aa9be4d311a78f24f76"), 
       "sch_end" : ISODate("2017-04-23T00:00:00.000Z"), 
       "sch_start" : ISODate("2017-04-23T00:00:00.000Z"), 
       "available" : false, 
       "title" : "test6" 
      } 
     ], 
     [ 
      { 
       "_id" : ObjectId("58f76aa9be4d311a78f24f78"), 
       "sch_end" : ISODate("2017-04-24T00:00:00.000Z"), 
       "sch_start" : ISODate("2017-04-24T00:00:00.000Z"), 
       "available" : false, 
       "title" : "test9" 
      }, 
      { 
       "_id" : ObjectId("58f76aa9be4d311a78f24f77"), 
       "sch_end" : ISODate("2017-04-25T00:00:00.000Z"), 
       "sch_start" : ISODate("2017-04-25T00:00:00.000Z"), 
       "available" : false, 
       "title" : "test10" 
      }, 
      { 
       "_id" : ObjectId("58f76aa9be4d311a78f24f76"), 
       "sch_end" : ISODate("2017-04-27T00:00:00.000Z"), 
       "sch_start" : ISODate("2017-04-27T00:00:00.000Z"), 
       "available" : false, 
       "title" : "test11" 
      } 
     ] 
    ], 

enter image description here

+0

您的屬性在'current_schedules [0] .length'處的值是一個js對象,而js對象沒有任何稱爲'length'的屬性。你可以使用像Object.keys(current_schedules [0]).length這樣的東西。 – Gaurav

+0

不,他顯然有陣列數組:'[[{...},{...},{...}],[{...},{...},{...} ]]' – Leguest

+1

@Leguest我們不知道OP是否在預先過濾數據。但通過查看'console.log',它顯然看起來像一個對象。 – Gaurav

回答

3

你的財產的,在current_schedules[0]值是一個JS對象和JS對象沒有叫長度的任何財產。你可以使用像Object.keys(current_schedules[0]).length這樣的東西。

注意:對於IE9 +和所有其他現代ES5 +功能的瀏覽器。

+0

代碼示例是錯誤的,看轉儲的屏幕截圖 –

+0

hi @gaurav您的建議工作,但只有0級陣列我有2個數組這裏是代碼請檢查... console.log(Object .keys(current_schedules [0])的長度)。 (var i = 0; i

相關問題