javascriptangularjsangularjs-controllerangularjs-filterng-filter

Angular JS Controller with Filter For JSON Array Of Objects


I have below kind of JSON and I wanted to find the number of objects with Status : 1 in JSON and I have tried with below approach and its not working .

And I know ng-filter needs to applied only to Array but I am not finding a way to do it in the below JSON data, Can someone help me

I am getting error as expected but how to resolve this issue in my case

[filter:notarray] Expected array but received:

Plunker Demo

In the Below JSON Data As you see I have Array with Arrays and I should get the Count of Status with value 1

Expected Output

Count Of 1st Array : 1 Count of 2nd Array : 3 Count of 3rd Array : 1

JSON Data

    [
  [{
    "id": 252323,
    "orderId": 223505,
    "status": 1,
    "executedOn": "13/Sep/17",
    "executedBy": "Person A",
    "executedByDisplay": "Person A",
    "cycleId": 3994,
    "cycleName": "Cycle A",
    "versionId": 21291,
    "versionName": "Version A",
    "issueKey": "Key-12561"
  }],
  [{
    "id": 253077,
    "orderId": 224047,
    "status": 1,
    "executedOn": "26/Sep/17",
    "executedBy": "Person B",
    "executedByDisplay": "Person B",
    "cycleId": 4012,
    "cycleName": "Cycle B",
    "versionId": 22912,
    "versionName": "Version B",
    "issueKey": "Key-12580"
  }, {
    "id": 253076,
    "orderId": 224046,
    "status": 2,
    "executedOn": "20/Sep/17",
    "executedBy": "Person B",
    "executedByDisplay": "Person B",
    "cycleId": 4012,
    "cycleName": "Cycle B",
    "versionId": 22912,
    "versionName": "Version B",
    "issueKey": "Key-12578"
  }, {
    "id": 253075,
    "orderId": 224045,
    "status": 1,
    "executedOn": "20/Sep/17",
    "executedBy": "Person B",
    "executedByDisplay": "Person B",
    "cycleId": 4012,
    "cycleName": "Cycle B",
    "versionId": 22912,
    "versionName": "Version B",
    "issueKey": "Key-12582"
  }, {
    "id": 253074,
    "orderId": 224044,
    "status": 1,
    "executedOn": "20/Sep/17",
    "executedBy": "Person B",
    "executedByDisplay": "Person B",
    "cycleId": 4012,
    "cycleName": "Cycle B",
    "versionId": 22912,
    "versionName": "Version B",
    "issueKey": "Key-12584"
  }],
  [{
    "id": 255168,
    "orderId": 226138,
    "status": 1,
    "executedOn": "26/Sep/17",
    "executedBy": "Person A",
    "executedByDisplay": "Person A",
    "cycleId": 4022,
    "cycleName": "Cycle C",
    "versionId": 21291,
    "versionName": "Version A",
    "issueKey": "Key-12617"
  }]
]

Angular JS Code

var app = angular.module('studentApp', []);

app.controller('StudentCntrl', function($scope, $http, $filter) {
  $http.get('data.json').then(function(response) {

    $scope.value = response.data;

    $scope.value.forEach(function(items) {
      $scope.eachValue = items;
      items.forEach(function(insideItems) {
        passvalue = $filter('filter')(insideItems, function(inputs) {
          if (inputs.status == '1')
            console.log(inputs.length);
          return inputs;
        });
      });

    });
  });
});

Solution

  • you can simply use java script array filter something like

     arr.filter( function(a){return a.status==1}).length
    

    Or using angular

     $filter('filter')(v, function(vc) {return vc.status ==1}).length
    

    Here is working demo