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.
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(',');
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