2017-08-13 111 views
1

我现在面临的问题与得到的参数名称获取JSON对象节点数在角JS

在下面的JSON具有特定值的JSON对象的计数我想获得具有数名:正在为你能帮我让我知道这里出了什么问题。下的问题>领域>状态

名称参数存在

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

     }) 
    } 

    } 
]); 

回答

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); 
      }) 
     } 
    } 
]); 

工作Plunkerhttps://plnkr.co/edit/ZOEN3X7Nt1onNeaHMp0H?p=preview

+1

感谢@Vivz像魅力一样工作 – Batman

1

要获得count,您可以简单地通过数组iterate并检查特定值发生的次数。当数值为IN PROGRESS时,我已使用reduce来计算并返回总和。

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());

+0

谢谢@Anurag辛格Bisht – Batman