2016-11-01 4 views
0
와 개체의 수를 얻는 방법

사람이 나는 상태에 제출 얼마나 많은 알고 싶어 JSON 아래에서 상태 필드각도에서 JSON에서 특정 값 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; 

     }) 
    }, 
]); 
을 포함하고 여기에 (Anular JS에서

각도 JS를 값을 얻을 수있는 방법

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

[배열의 일부 속성이있는 항목 수 얻기] 가능한 복제본 (http://stackoverflow.com/questions/15360256/get-count-of-items-with-some-property-in-an-array)) – manzapanza

+0

@manzapanza 여기 JSON 데이터에서 이러한 종류의 질문을 수행했습니다. 많은 필드가 있습니다. 함수가 Status = Count 등의 수치를 처리해야하는 방법이 있습니까? – Batman

답변

3

당신은 각도의 $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; 

필터를 사용하면 특정 상태의 개체로만 배열이 축소되므로 배열의 수를 계산할 수 있습니다.

+0

감사합니다. Status = Assigned 인 값이 없다고 가정하면 '0'으로 반환 할 방법이 있습니다. – Batman

+1

필터와 일치하는 것이 없으면 빈 배열을 반환해야하므로 카운트는 0이됩니다. – Ankh

관련 문제