0

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});

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do:

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

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

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.