我正在使用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. StateInformations

UPD:

根据您对我的答案的评论,我判断您可能需要自定义过滤器才能获得所需的内容,也可以使用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/

10-12 14:44