Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make angular $filter to filter a case sensitive string comparators

I have been trying to make the filter in angular controller which is case sensitive while filtering the array.

My data is as below:

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 below:

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

My filter in controller is:

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

Here the trap is that when i enter "diamond" in input field

$scope.propertyName = "diamond";

the filter doesn't match this string with the "Diamond".

I don't want to remove the exact match condition (true) from the equation as shown below:

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

This won't work for me, because i want to match the exact string to filter the data. And the data will have unique "stone_name" values. Also i don't want to use any loops since the array length will go above 1000+. Is there anyway i can achieve this?

like image 770
Srini Vas Avatar asked Oct 18 '22 01:10

Srini Vas


1 Answers

Try using a match function to get case insensitive match:

var stoneObj = $filter('filter')(stoneArr, function (item) {
    return item.stone_name.toLowerCase() == $scope.propertyName.toLowerCase();
},true); 
like image 177
F.Igor Avatar answered Oct 20 '22 22:10

F.Igor