Reputation: 771
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.
});
Upvotes: 0
Views: 152
Reputation: 16068
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
Upvotes: 3