【问题标题】:Get the JSON Object Node Count in Angular JS获取 Angular JS 中的 JSON 对象节点数
【发布时间】:2017-08-13 05:01:04
【问题描述】:

我在获取参数名称具有特定值的 JSON 对象的计数时遇到问题

在下面的 JSON 中,我想获得名称为 2 的计数:IN PROGRESS。请您帮我告诉我这里出了什么问题。

name参数出现在issues>fields>status下

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"
        }
      }
    }
  ]
}

角度 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);

      })
    }

  }
]);

【问题讨论】:

    标签: angularjs json angularjs-filter


    【解决方案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);
                })
            }
        }
    ]);
    

    打工者https://plnkr.co/edit/ZOEN3X7Nt1onNeaHMp0H?p=preview

    【讨论】:

      【解决方案2】:

      要获得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());

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 2016-04-15
        • 2015-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多