2017-08-13 89 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