mpsbhat mpsbhat - 5 months ago 23
AngularJS Question

angularjs filter based on particular key (exact match)

I want to filter an angularjs array say,

$scope.myObj = [
{id:1,name:'foo'},
{id:2,name:'bar'},
{id:10,name:'quad'},
{id:13,name:'cad'}
]


based particularly on
id = 1
. If i use

console.log($filter('filter')($scope.myObj, 1));


it will return all the object that contain
1
. How can i restrict to filter only required object?

EDIT: It can solved by using `true for exact match based on answer below.

console.log($filter('filter')($scope.myObj, {id: 1}, true));

Answer

Execute below code to filter data by ID column:

var filterObjList=$filter('filter')($scope.myObj, {id:1}, true);
if(filterObjList && filterObjList.length>0)
return filterObjList[0];

If you want to filter the list by name then replace "Id" with "name" and pass the value in single inverted commas.