如何在Angular JS中获取JSON中具有特定值的Object的计数

时间:2016-11-01 13:39:06

标签: angularjs json

任何人都可以帮助我获取状态字段

的计数

从JSON下面我想知道有多少归档了Status = Open and Closed和Assigned

如何在Anular JS中获取值

Angular JS(这里我从URL获取JSON值,它包含在JSON数据下面

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": [
    {
      "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"
    }
  ]
}

1 个答案:

答案 0 :(得分:3)

您可以使用Angular的$filter服务。

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;

过滤器会将数组简化为具有特定状态的对象,然后您只需计算数组中的数字。