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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With