Archipel Archipel - 6 months ago 12
AngularJS Question

$filter to find single item with ID (AngularJS)

I'm trying to find an item in an array with an ID.
I get that ID from my url and I tried to use $filter to extract it from my array.

Here's what my code looks like :

$filter('filter')($rootScope.items,{ID_ITEM:$location.$$path.replace('/','')})


so bassically:

$filter('filter')($rootScope.items,{ID_ITEM:id})


It works but the problem is that it also returns other item with the same number in the ID

Example: If my url is #/3, it will return the object with id 23,13,3,33 etc...

How can I filter my array to only find the exact one ?

Answer

Just pass the true option to match exactly as the third parameter to the filter:

$filter('filter')($rootScope.items,{ID_ITEM:$location.$$path.replace('/','')},true)
Comments