How to check lowercase letters 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 something like this

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

but it throws an error because $scope.diseaseList- this is an array. I can delete indexOfand use another loop, but I do not want to do this. Any other option please suggest.

+4
source share
1 answer

Arrays do not have toLowerCase(note that your code has a typo: missing function r). But you can use the function mapand return string values. It works as follows:

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

In your code, this can be applied as shown below:

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

, != -1 , :

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

@Tushar :

String.prototype.toLowerCase.apply(arr).split(',');
+8

Source: https://habr.com/ru/post/1610412/


All Articles