[英]Angularjs : Filter an array with $filter
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.以优雅的方式使用 $filter('filter') 但我不知道如何做到这一点。 If anybody knows...
如果有人知道...
Thanks谢谢
Following will return object which not includes state1 and state2以下将返回不包括 state1 和 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);
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.