Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check lowercase in indexOf

Here diseaseList is an array .

for(var i=0;i<_list.length;i++)
{
    if($scope.diseaseList.indexOf(_list[i].disease) != -1){
        _list[i].favorite = true;
    }else{
        _list[i].favorite = false;
    }
}

I want to do somthing like this

if($scope.diseaseList.toLoweCase().indexOf(_list[i].disease.toLoweCase()) != -1){

but it is throwing error as $scope.diseaseList is an array .I can remove indexOf and use one more loop but that I dont want to do .Any other option please suggest.

like image 631
Prashobh Avatar asked Feb 09 '23 21:02

Prashobh


1 Answers

Arrays don't have toLowerCase (note that in your code there is a typo: missing r) function. But you can use the map function and return the lowercase values. It works like this:

["Foo", "BaR"].map(function (c) { return c.toLowerCase(); });
// => ["foo", "bar"]

In your code, this can be applied like below:

if($scope.diseaseList.map(function (c) {
    return c.toLowerCase();
   }).indexOf(_list[i].disease.toLowerCase()) != -1) { ... }

And additionally, you can remove != -1 and change it to use the bitwise operator:

if(~$scope.diseaseList.map(function (c) {
    return c.toLowerCase();
   }).indexOf(_list[i].disease.toLowerCase())) { ... }

@Tushar has another interesting solution for converting an array of strings to lowercase:

String.prototype.toLowerCase.apply(arr).split(',');
like image 142
Ionică Bizău Avatar answered Feb 11 '23 13:02

Ionică Bizău