Say I have the following array:
var x = [{letter: 'A', number: 1}, {letter: 'B', number: 2}, {letter: 'C', number: 3}]
And I want to filter using this array:
var f = ['A', 'B']
So that my resulting array looks like this:
[{letter: 'A', number: 1}, {letter: 'B', number: 2}]
How can I accomplish this using javascript in AngularJS? I tried this, but no luck:
$filter('filter')(x, {letter: f});
If you would like to use AngularJS's Filter module then you can do the following:
var x = [{letter: 'A', number: 1}, {letter: 'B', number: 2}, {letter: 'C', number: 3}]
var f = ['A', 'B']
function filterExp(value,index) {
return (f.indexOf(value.letter) > -1)
}
$scope.filtered = $filter('filter')(x, filterExp, true);
I have created a JSFiddle to demonstrate its result.
You can do:
var filteredElems = x.filter(function(obj) {
return f.indexOf(obj.letter) > -1
});
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.