2

I want to do :

myList.forEach(function(element){
   if (!['state1', 'state2'].contains(element.state)){
     myFilteredList.push(element)
   }
})

in an elegant way with $filter('filter') but I don't see how I can do that. If anybody knows...

Thanks

2 Answers 2

2

Following will return object which not includes state1 and state2

myList = [{ state: 'state1', p: 'csss' }, { state: 'state2', p: 'cppss' }, { state: 'state2', p: 'csssss' }, { state: 's1', p: 'csaas' }]
myFilteredList = [];
x = $filter('filter')(myList, (item) => {
    return !['state1', 'state2'].includes(item.state)
})
myFilteredList.push(x)
console.log(myFilteredList);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

myFilteredList = $filter('filter')(myList, function(element){
    return ['state1', 'state2'].indexOf(element.state) == -1
})

angular.module("app", [])
  .controller("ctrl", function($scope, $filter) {
    var myList = [{
        state: 'state1',
        prop: 'prop1'
      },
      {
        state: 'state2',
        prop: 'prop2'
      },
      {
        state: 'state2',
        prop: 'prop3'
      },
      {
        state: 'state3',
        prop: 'prop4'
      },
      {
        state: 'state4',
        prop: 'prop5'
      }
    ]

    $scope.myFilteredList = $filter('filter')(myList, function(element) {
      return ['state1', 'state2'].indexOf(element.state) == -1
    })
  })
<!DOCTYPE html>
<html ng-app="app">

<head>
</head>

<body ng-controller="ctrl">
  <pre>{{ myFilteredList | json }}</pre>
  <script src="https://code.angularjs.org/1.6.4/angular.min.js"></script>
</body>

</html>

Comments

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.