javascript - 如何使用angularjs根据特定值拼接数组

标签 javascript angularjs arrays

我正在使用 angularjs 并且我在范围内有一个数组。我需要删除范围数组中 No=1 的对象,其中有大量数据。我需要删除称为“1”的特定值。

请帮助实现这一目标。

    var data=[
      {
         "No":1,
        "PatientState": "AK",
        "DispenseMonth": "7/1/2016" 

      },
      {
        "No":2,
        "PatientState": "AK",
        "DispenseMonth": "8/1/2016" 
      },
      {
         "No":1,
        "PatientState": "AK",
        "DispenseMonth": "9/1/2016" 
      },
      {
        "No":1,
        "PatientState": "AK",
        "DispenseMonth": "10/1/2016" 
      },
      {
        "No":4,
        "PatientState": "AK",
        "DispenseMonth": "11/1/2016" 
      },
      {
        "No":1,
        "PatientState": "AK",
        "DispenseMonth": "2/1/2017" 
      },
      {
         "No":5,
        "PatientState": "AK",
        "DispenseMonth": "3/1/2017" 
      }
        ]

        $scope.StateInformations =data;

最佳答案

使用Array.filter 来过滤你想要的,并将过滤结果设置回$scope。状态信息

更新:

根据您对我的回答的评论,我判断您可能需要一个自定义过滤器来获得您想要的内容,您也可以将 Array.filter 用于自定义过滤器。

引用下面的代码片段和这个 plunker demo .

var app = angular.module("app", []);
app.controller("myCtrl", function($scope) {
  $scope.conditions = [];
  $scope.options = [{
    check: false,
    value: 1
  }, {
    check: false,
    value: 2
  }, {
    check: false,
    value: 3
  }];
  $scope.data = [{
      "No": 1,
      "PatientState": "AK",
      "DispenseMonth": "7/1/2016"
    },
    {
      "No": 2,
      "PatientState": "AK",
      "DispenseMonth": "8/1/2016"
    },
    {
      "No": 1,
      "PatientState": "AK",
      "DispenseMonth": "9/1/2016"
    },
    {
      "No": 1,
      "PatientState": "AK",
      "DispenseMonth": "10/1/2016"
    },
    {
      "No": 4,
      "PatientState": "AK",
      "DispenseMonth": "11/1/2016"
    },
    {
      "No": 1,
      "PatientState": "AK",
      "DispenseMonth": "2/1/2017"
    },
    {
      "No": 5,
      "PatientState": "AK",
      "DispenseMonth": "3/1/2017"
    }
  ];

  $scope.setFilterCondition = function(option) {
    if (option.checked) {
      $scope.conditions.push(option.value);
    } else {
      $scope.conditions.splice($scope.conditions.indexOf(option.value), 1);
    }
  };
});
app.filter("sampleFilter", function() {
  return function(input, condition) {
    if (!input) { return []; }
    if (!condition || condition.length === 0)  { return input; }

    return input.filter(function(item) {
      for (var i = 0; i < condition.length; i++) {
        if (item.No === condition[i]) {
          return true;
        }
      }
      return false;
    });
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  <div ng-repeat="option in options">
    <label>
      <input type="checkbox" ng-model="option.checked" ng-change="setFilterCondition(option)">
      {{option.value}}
    </label>
  </div>
  <br>
  <div ng-repeat="item in data | sampleFilter: conditions">
    <span>{{item.No}}</span> -
    <span>{{item.PatientState}}</span> -
    <span>{{item.DispenseMonth}}</span>
  </div>
</div>

关于javascript - 如何使用angularjs根据特定值拼接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44109725/

相关文章:

javascript - 社交网络在索引时可以运行 JavaScript 吗?

javascript - 关于函数返回数组数据的问题

javascript - 如何在 vuejs 中使用数据初始化应用程序

angularjs - 如何检测 Angular js中多维数组模型的变化

javascript - 匹配行首时 JavaScript V8 正则表达式引擎中的错误?

javascript - 从选择 Angularjs 调用 Controller 函数

python - 如何将滑动窗口的 3d 数组展平为 2d 数组?

javascript - 使用 ng-src 访问对象内部的数组

javascript - 为什么 document.body 在我的 javascript 中为空?

javascript - 如何使用 JS 检查从 PHP 返回的空值?