2016-05-22 125 views
0

我正在使用GET方法來查看json文件,如下所示。計算json數組中的對象數

$scope.train = function() { 

      var url = 'http://localhost/heart/api/restApiController/dataset.json'; 

      $http({ 
       method: 'GET', 
       url: url 


      }).then(function (data) { 

      }); 


     }; 

現在我想要統計json文件中的對象數量,我可以做到嗎?

日誌數據

Object {data: Array[5], status: 201, config: Object, statusText: "Created"} 
config:Object 
data:Array[5] 
headers:(d) 
status:201 
statusText:"Created" 

所以有這array.how 5個對象,我可以得到AngularJS代碼這個值5?

+0

看到這個http://stackoverflow.com/questions/28716541/how-to-count-map-size-in-angularjs和http://計算器.com/questions/5223/length-of-javascript-object-that-is-associative-array –

回答

2

你可以簡單地使用標準length

data.data.length 
+0

謝謝!有效。 – CraZyDroiD

+0

所以如果我想用特定的值來計算對象,我該怎麼做? 例如:我想要計算重量爲「2」的對象 如何做到這一點? – CraZyDroiD

+0

在使用angularjs時,可以在返回的數組上使用'filter'和'length',參見[filter](https://docs.angularjs.org/api/ng/filter/filter) –