json - angularjs count duplicate values in array of objects -
i'm working angularjs. have array of objects this:
$scope.documents = [ { "id": "221", "activate": "t" }, { "id": "1", "activate": "t" }, { "id": "2", "activate": "t" }, { "id": "221", "activate": "t" }, { "id": "5", "activate": "t" }, { "id": "221", "activate": "t" }, { "id": "221", "activate": "t" }, { "id": "7", "activate": "t" }, { "id": "8", "activate": "t" }, { "id": "9", "activate": "t" }, { "id": "221", "activate": "t" } ]
i need angularjs function count number of repeated values in array of objects.
something this:
$scope.count = function(param) { angular.foreach($scope.preguntas, function(value, key) { if (value.id == param){ ........ ........ } }); };
but i'm not sure how can do.
i hope result this:
count(221); 5
any of have suggestions please?
just increment count variable in if statement , return it.
$scope.count = function(param) { var count = 0; $scope.documents.foreach(function(document) { if(document.id === param.tostring() { count++; } }); return count; };
and markup:
<div>{{count(221)}}</div>