Pra*_*obh 4 javascript jquery angularjs
这里的疾病列表是一个数组.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情
if($scope.diseaseList.toLoweCase().indexOf(_list[i].disease.toLoweCase()) != -1){
Run Code Online (Sandbox Code Playgroud)
但它正在抛出错误,因为$scope.diseaseList是一个数组.我可以删除indexOf并使用一个循环,但我不想这样做.任何其他选项请建议.
数组没有toLowerCase(注意在你的代码中有一个拼写错误:缺失r)函数.但您可以使用该map函数并返回小写值.它的工作原理如下:
["Foo", "BaR"].map(function (c) { return c.toLowerCase(); });
// => ["foo", "bar"]
Run Code Online (Sandbox Code Playgroud)
在您的代码中,这可以应用如下:
if($scope.diseaseList.map(function (c) {
return c.toLowerCase();
}).indexOf(_list[i].disease.toLowerCase()) != -1) { ... }
Run Code Online (Sandbox Code Playgroud)
此外,您可以删除!= -1并更改它以使用按位运算符:
if(~$scope.diseaseList.map(function (c) {
return c.toLowerCase();
}).indexOf(_list[i].disease.toLowerCase())) { ... }
Run Code Online (Sandbox Code Playgroud)
@Tushar有另一个有趣的解决方案,用于将字符串数组转换为小写:
String.prototype.toLowerCase.apply(arr).split(',');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
800 次 |
| 最近记录: |