Batman Batman - 1 month ago 7
JSON Question

How to get the count of the Object with specific value in JSON in Angular JS

Can anyone help me in getting the counts of the Status Field

From Below JSON I wanted to know how many filed with Status = Open and Closed and Assigned

How can get the values in Anular JS

Angular JS (Here I am getting the JSON value from URL and it contains below JSON Data

var app = angular.module('defectApp', ["ngTable"]);
app.controller('defectController', [
'$scope',
'$http',
function($scope, $http) {
$http({method: 'POST', url: '/angular/defect/defect.json'}).success(function(response) {
$scope.post = response;

})
},
]);


JSON Data

{
"data": [
{
"type": "defect",
"id": 1,
"user-01": "John",
"status": "Open",
"severity": "1 - Urgent"
},
{
"type": "defect",
"id": 2,
"user-01": "John",
"status": "Open",
"severity": "3 - High"
},
{
"type": "defect",
"id": 3,
"user-01": "John",
"status": "Assigned",
"severity": "3 - High"
},
{
"type": "defect",
"id": 4,
"user-01": "John",
"status": "Closed",
"severity": "4 - Medium"
},
{
"type": "defect",
"id": 5,
"user-01": "John",
"status": "Closed",
"severity": "4 - Medium"
}
]
}

Answer

You could use Angular's $filter service.

var openCount = $filter('filter')(response.data, {status: 'Open'}).length,
    closedCount = $filter('filter')(response.data, {status: 'Closed'}).length,
    assignedCount = $filter('filter')(response.data, {status: 'Assigned'}).length;

The filter will reduce the array to just the objects with a particular status, then you can just count the number in the array.