在Angular JS中获取JSON对象节点计数

时间:2017-08-13 05:01:05

标签: angularjs json angularjs-filter

我遇到了让JSON对象的计数具有参数名称

的特定值的问题

在JSON下面,我想得到一个名字:IN PROGRESS为2.你能帮我告诉我这里出了什么问题。

name参数出现在问题>字段>状态

Plunker

JSON数据

 {
  "expand": "schema,names",
  "startAt": 0,
  "maxResults": 50,
  "total": 257,
  "issues": [
    {
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "1579217",
      "fields": {
        "created": "2017-08-11T13:03:52.000+0000",
        "resolutiondate": null,
        "status": {
          "name": "IN PROGRESS",
          "id": "3"
        }
      }
    },
    {
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "1578077",
      "fields": {
        "created": "2017-08-10T13:14:53.000+0000",
        "resolutiondate": null,
        "status": {
          "name": "IN PROGRESS",
          "id": "10548"
        }
      }
    },
    {
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "1562078",
      "fields": {
        "created": "2017-07-27T03:42:24.000+0000",
        "resolutiondate": null,
        "status": {
          "name": "To Do",
          "id": "10548"
        }
      }
    }
  ]
}

Angular JS

var app = angular.module('myApp', ['angular-loading-bar']);

app.controller("Controller", ["$scope", "$http", "$filter", "$window",
  function($scope, $http, $window, $filter) {

    $scope.findTheValue = function() {

      $http({
        method: 'POST',
        url: 'issues.json'
      }).then(function(response) {

        $scope.selectedCount = $filter('filter')(response.issues, {
          name: 'IN PROGRESS'
        }).length;

        console.log($scope.selectedCount);

      })
    }

  }
]);

2 个答案:

答案 0 :(得分:1)

要获取count,您只需iterate通过数组并检查特定值发生的次数。我使用reduce来获取计数,并在值为IN PROGRESS时返回总和。

var data = {
  "expand": "schema,names",
  "startAt": 0,
  "maxResults": 50,
  "total": 257,
  "issues": [{
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "1579217",
      "fields": {
        "created": "2017-08-11T13:03:52.000+0000",
        "resolutiondate": null,
        "status": {
          "name": "IN PROGRESS",
          "id": "3"
        }
      }
    },
    {
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "1578077",
      "fields": {
        "created": "2017-08-10T13:14:53.000+0000",
        "resolutiondate": null,
        "status": {
          "name": "IN PROGRESS",
          "id": "10548"
        }
      }
    },
    {
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "1562078",
      "fields": {
        "created": "2017-07-27T03:42:24.000+0000",
        "resolutiondate": null,
        "status": {
          "name": "To Do",
          "id": "10548"
        }
      }
    }
  ]
};

function getCount() {
  return data.issues.reduce((acc, curr) => {
    if(curr.fields.status.name === 'IN PROGRESS') {
      return acc + 1;
    }
    return acc;
  },0);
}

console.log(getCount());

答案 1 :(得分:1)

上述代码中的错误很少

首先,依赖注入的顺序不正确。

app.controller("Controller", ["$scope", "$http","$filter", "$window",
  function($scope, $http, $filter, $window) {

其次,由于您使用的是POST,因此必须将一些数据传递给API调用。将空数据{}传递到POST或使用GET

第三,您必须访问回复的数据属性,例如response.data.issues

var app = angular.module('myApp', []);
app.controller("Controller", ["$scope", "$http", "$filter", "$window",
    function($scope, $http, $filter, $window) {
        $scope.findTheValue = function() {
            $http({
                method: 'GET',
                url: 'issues.json'
            }).then(function(response) {
                $scope.selectedCount = $filter('filter')(response.data.issues, function(
                    inputs) {
                    if (inputs.fields.status.name == 'IN PROGRESS') return inputs;
                });
                console.log($scope.selectedCount.length);
            })
        }
    }
]);

Working Plunker https://plnkr.co/edit/ZOEN3X7Nt1onNeaHMp0H?p=preview