2015-03-31 495 views
0

我試圖獲取數組的大小,但每次控制檯都說我未定義,並且不知道爲什麼。在Angular-JS中獲取數組大小

.success(function (data) { 
      console.log('Random-Suffix:' + suffix); 
      $scope.builds = new X2JS().xml_str2json(data); 

      var size = $scope.builds.length; 
      console.log('Size: ' + size); 



      $timeout(function() { 
       $scope.exampleLiveDataChange(); 
      }, 3000); 
     }) 
+0

'console.log($ scope.builds)'的輸出是什麼? – Manwal 2015-03-31 08:48:16

+0

嘗試'console.log(typeof($ scope.builds))''你很可能會看到'$ scope.builds'不是一個數組!您可能需要輸出它並在其中查找您的數組; – Fourat 2015-03-31 09:07:20

回答

2

從我在讀X2JS documentation

<instance>.xml_str2json - Convert XML specified as string to JSON 

也許建立不是一個數組,而是一個對象。如果對象包含這樣一個數組:

var test = {one : 'one' , two : [1,2,3]}; 

如果你這樣做:

test.length; 

你得到:

undefined 

如果你這樣做:

test.two.length; 

然後你會得到:

3 
+0

是的,你說得對,謝謝。 你知道是否有辦法獲得對象內數組的大小? – Vistor 2015-03-31 08:52:20

+0

是的,很簡單。對象的結構是什麼? – namelivia 2015-03-31 08:52:58

+0

它的工作原理,謝謝 – Vistor 2015-03-31 09:08:23