1

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;

1 Answer 1

5

use Array.filter to filter what you want and set filter result back to $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>

Sign up to request clarification or add additional context in comments.

12 Comments

i have checkbox 1 ,2 ,3 like when Uncheck first one i need to filter NO:1 two Uncheck means filter NO:2
understand @pengyy or otherwise i create a fiddle it's easy to uderstand t
@jose you should post all your requirements in your question at once.
@jose I guess you may need a custome filter. wait a moment.
@jose see the updated example. I remembered answered similar question before. but didn't find it. never mind, I just made a new example. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.