0

I am trying to get the exact key match of object array in angularjs instead of that I am getting all matching results. How can i achieve that?

here is the controller code:-

var myApp = angular.module('myApp', []);

myApp.controller('ToddlerCtrl', function ($scope,$filter) {


 $scope.toddlers = [
{
  "name": "Toddler One",
  "tid": "85",

},
{
  "name": "Toddler Two",
  "tid": "485",

},
{
  "name": "Toddler Three",
  "tid": "4485",

} ,
{
  "name": "Toddler Four",
  "tid": "8845",

}
];

  var found = $filter('filter')($scope.toddlers, 85  );
    console.log(found); // I want the exact match i.e. ("Toddler one" with "85" only) but I am getting 3 results.



});

here is the Plnkr

1 Answer 1

3

The solution is quite easy: Add a true as a third param to indicate that the search needs be exact. Note that 85 won´t match anything though, you need to put '85':

var found = $filter('filter')($scope.toddlers, '85' , true);

For more info check the comparator parameter in docs: Angular Filter

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.