Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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});
share|improve this question

2 Answers 2

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.

share|improve this answer

You can do:

var filteredElems = x.filter(function(obj) {
    return f.indexOf(obj.letter) > -1
});

Demo: http://jsfiddle.net/5g6hjyzx/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.