1

When I filter an array of object with a number something like:

$scope.users = [{name:'John', id:-1},
                         {name:'Mary', id:1},
                         {name:'Mike', id:2},
                         {name:'Adam', id:3}];

var selected = $filter('filter')($scope.users, {id: $scope.target});

If target = 1, the selected result will include John and Mary. But I only want the exactly same id which is Mary with id 1 to be included. Why does it include negative numbers? How can I prevent that?

1 Answer 1

4

You can set the third parameter of the filter which is the comparator to be true

$scope.users = [{name:'John', id:-1},
                         {name:'Mary', id:1},
                         {name:'Mike', id:2},
                         {name:'Adam', id:3}];

var selected = $filter('filter')($scope.users, {id: $scope.target}, true);

true is a shorthand for:

function(actual, expected) { 
    return angular.equals(actual, expected)
} // This is essentially strict comparison of expected and actual.

Plunkr.

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

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.