带有JSON对象数组的过滤器的Angular JS控制器

时间:2017-10-06 04:02:16

标签: javascript angularjs angularjs-controller angularjs-filter ng-filter

我有以下类型的JSON,我想在JSON中找到Status : 1的对象数量,我尝试使用以下方法,但它无效。

我知道ng-filter只需要应用于Array但是我没有找到一种方法在下面的JSON数据中做到这一点,有人可以帮助我吗

我按预期收到错误,但如何在我的案例中解决此问题

[filter:notarray] Expected array but received:

Plunker Demo

在下面的JSON数据中你看到我有阵列数组,我应该得到 计数状态值为1

预期输出

第一阵列计数:1 第二阵列计数:3 第三阵列的数量:1

JSON数据

    [
  [{
    "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;
        });
      });

    });
  });
});

1 个答案:

答案 0 :(得分:2)

您可以简单地使用java script array filter之类的内容

15 --> [2, 3, 5, 7, 11, 13]
10 --> [2, 3, 5, 7]

或使用角度

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

Here is working demo