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;
AT82
  • 71,416
  • 24
  • 140
  • 167
jose
  • 1,044
  • 1
  • 12
  • 35

1 Answers1

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>
Pengyy
  • 37,383
  • 15
  • 83
  • 73
  • i have checkbox 1 ,2 ,3 like when Uncheck first one i need to filter NO:1 two Uncheck means filter NO:2 – jose May 22 '17 at 10:38
  • understand @pengyy or otherwise i create a fiddle it's easy to uderstand t – jose May 22 '17 at 11:14
  • @jose you should post all your requirements in your question at once. – Pengyy May 22 '17 at 11:15
  • @jose I guess you may need a custome filter. wait a moment. – Pengyy May 22 '17 at 11:15
  • @jose see the updated example. I remembered answered similar question before. but didn't find it. never mind, I just made a new example. :) – Pengyy May 22 '17 at 11:34
  • @ Pengyy functionality is fine but my need is i am need not to show in ng-repeat i show console.log when i is checked remaing value or shown in log.there is possible to catch my approch? – jose May 22 '17 at 11:45
  • @jose which one is not in ng-repeat? the checkboxes? the result? – Pengyy May 22 '17 at 11:48
  • my result is look like this but without 'app.filter' possible to do – jose May 22 '17 at 11:50
  • @jose you can just modify the filter to a function in your controller. it's the same. – Pengyy May 22 '17 at 11:51
  • shall we discuss in chat?@pengyy – jose May 22 '17 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144837/discussion-between-pengyy-and-jose). – Pengyy May 22 '17 at 12:21
  • https://stackoverflow.com/questions/44601184/excel-not-properly-generate-in-angularjs/44601238#44601238 @Pengyy – jose Jun 17 '17 at 06:08