How to make angular $ filter to filter random string comparators

I am trying to make a filter in an angular controller that is case sensitive when filtering an array.

My data is as follows:

var stoneArr = [ { "stone_name": "Diamond", "id": 16 }, { "stone_name": "Ruby", "id": 17 }, { "stone_name": "Sapphire", "id": 18 }, { "stone_name": "Emerald", "id": 19 } ]; 

My HTML input is as follows:

 <input type="text" name="stone_name" class="form-control" id="stone_name" ng-model="propertyName" maxlength="15" required> 

My filter in the controller:

 var stoneObj = $filter('filter')(stoneArr, {stone_name:$scope.propertyName}, true); 

The trap here is that when I enter a β€œdiamond” in the input field

 $scope.propertyName = "diamond"; 

the filter does not match this line with "Diamond".

I do not want to remove the exact matching condition (true) from the equation, as shown below:

 var stoneObj = $filter('filter')(stoneArr, {stone_name:$scope.propertyName}); //This won't work for me 

This will not work for me, because I want to combine the exact string to filter the data. And the data will have unique values ​​of "stone_name". Also, I do not want to use any loops, since the length of the array will exceed 1000+. Anyway, can I achieve this?

+5
source share
1 answer

Try using the match function to get case insensitive:

 var stoneObj = $filter('filter')(stoneArr, function (item) { return item.stone_name.toLowerCase() == $scope.propertyName.toLowerCase(); },true); 
+3
source

All Articles