Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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));
like image 694
mpsbhat Avatar asked Jun 13 '26 01:06

mpsbhat


1 Answers

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.

like image 68
Himanshu Jain Avatar answered Jun 17 '26 23:06

Himanshu Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!