简体   繁体   中英

how to splice the array based on paritcular value using angularjs

I'm using angularjs and I have an array in scope. And I need to remove objects where No=1 in scope array lots of data in there. I need to remove particular value is called '1'.

Please help how to achieve this.

    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;

use Array.filter to filter what you want and set filter result back to $scope. StateInformations $scope. StateInformations

UPD:

according your comments on my answer, I judged you may need a custom filter to get what you want, you can use Array.filter into custom filter also.

refer the below code snippet and this 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM