2017-07-16 35 views
-1

我想在javascript中訪問一個數組鍵值。在javascript對象內部訪問數組鍵值

這裏是我的.js文件:

var contentData = [ 
      { 
       tops: [ 
          { image: "images/top1.jpg" }, 
          { image: "images/top2.jpg" }, 
          { image: "images/top3.jpg" }, 
          { image: "images/top4.jpg" } ] 
      } 
    ]; 

$scope.objectArray = contentData; 
$scope.objectCount = $scope.objectArray[0].length; 

我試圖訪問「頂」內部「contentData內容」,顯示「頂部」數組的長度應該有四個

+0

應該是'$ scope.objectArray [0] .tops.length' –

回答

1
> var contentData = [ 
...    { 
...     tops: [ 
...        { image: "images/top1.jpg" }, 
...        { image: "images/top2.jpg" }, 
...        { image: "images/top3.jpg" }, 
...        { image: "images/top4.jpg" } ] 
...    } 
...  ]; 
undefined 
> 
> contentData[0].tops.length 
4 
+0

哦我看到了! @AndyGaskell工作! – Cameron

0

您不指定tops屬性。它應該是:

$scope.objectArray[0].tops.length;