javascript javascript中的.toLowerCase“不是函数”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26533977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.toLowerCase in javascript "not a function"?
提问by user3482415
I'm trying to do something like this
我正在尝试做这样的事情
if (pathArray.toLowerCase().indexOf("blah") != -1{}
When debugging with console I get the error that "pathArray.toLowerCase is not a function". Why do I get that message?
使用控制台进行调试时,我收到“pathArray.toLowerCase 不是函数”的错误消息。为什么我会收到这条消息?
回答by dfsq
toLowerCase
is a method of the string. If you want to be able to find a string in array without knowing exact case you can add map
step to the chain:
toLowerCase
是字符串的方法。如果您希望能够在不知道确切大小写的情况下在数组中找到一个字符串,您可以map
向链中添加步骤:
pathArray.map(function(s) { return s.toLowerCase(); }).indexOf('blah') !== -1
回答by jdussault
toLowerCase() is only for strings, but I have a feeling that your "pathArray" is, in fact, an array.
toLowerCase() 仅适用于字符串,但我感觉您的“pathArray”实际上是一个数组。
> 'hello'.toLowerCase()
'hello'
> ['hi', 'bye'].toLowerCase()
TypeError: undefined is not a function
Are you trying to check if "blah" exists in your array in any uppercase / lowercase form?
您是否正在尝试以任何大写/小写形式检查数组中是否存在“blah”?
回答by Augusto Altman Quaranta
The toLowerCasemethod belongs to the String function prototype. So probably pathArray isn't a String. I have the feeling (for its name) that is an Array. In that case the following code could be useful for you:
的toLowerCase方法属于字符串函数原型。所以可能 pathArray 不是字符串。我有一种感觉(因为它的名字)是一个数组。在这种情况下,以下代码可能对您有用:
pathArray.forEach(function(item, index){
if(item.toLowerCase().indexOf("blah") != -1){
}
});
The code proposed by dfsq could be useful too. It depends on what level you want to perform the indexOf function. In my case you will be performing search over each string in order to find the start index of the sub string "blah". In the dfsq's code you will be looking the array index which contains the the entire string "blah".
dfsq 提出的代码也很有用。这取决于您要执行 indexOf 函数的级别。在我的情况下,您将对每个字符串执行搜索以找到子字符串“blah”的起始索引。在 dfsq 的代码中,您将查看包含整个字符串“blah”的数组索引。
回答by Felix Kling
yes, it is an array so that makes sens now that it would not work. I'm trying to make indexOf. case insensitive.
是的,它是一个数组,因此现在它不起作用是有意义的。我正在尝试制作 indexOf。不区分大小写。
No need for Array#indexOf()
:
不需要Array#indexOf()
:
if (pathArray.some(function(v) { return v.toLowerCase() === 'blah';}))
Array#some()
returns true if for any element the callback returns true
.
Array#some()
如果回调返回任何元素,则返回 true true
。
回答by Johnny
Your String is probably resides in index 0 of an Array.
您的字符串可能位于数组的索引 0 中。
Appling toLowerCase() on the 0 index should solve your issue:
在 0 索引上应用 toLowerCase() 应该可以解决您的问题:
pathArray[0].toLowerCase()