最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.toLowerCase in javascript "not a function"? - Stack Overflow

programmeradmin9浏览0评论

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?

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?

Share Improve this question edited Oct 24, 2014 at 8:13 Sebastian Zartner 20.1k10 gold badges102 silver badges141 bronze badges asked Oct 23, 2014 at 17:35 user3482415user3482415 691 gold badge1 silver badge4 bronze badges 4
  • Is pathArray an array? – Dave Zych Commented Oct 23, 2014 at 17:39
  • pathArray is probably not a string. If it is an array, you probably want pathArray.some(function(v) { return v.toLowerCase() === 'blah';}); . – Felix Kling Commented Oct 23, 2014 at 17:39
  • .toLowerCase() is a function in String.prototype. Most likely your pathArray is a different type of object. – Dave Commented Oct 23, 2014 at 17:39
  • if pathArray is an array, toLowerCase() is a string method, you would need to traverse each element in array and convert it to lowercase. Also, you are missing a closing ) – juvian Commented Oct 23, 2014 at 17:39
Add a ment  | 

6 Answers 6

Reset to default 6

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:

pathArray.map(function(s) { return s.toLowerCase(); }).indexOf('blah') !== -1

toLowerCase() is only for strings, but I have a feeling that your "pathArray" is, in fact, an array.

> '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?

The toLowerCase method 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:

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".

yes, it is an array so that makes sens now that it would not work. I'm trying to make indexOf. case insensitive.

No need for Array#indexOf():

if (pathArray.some(function(v) { return v.toLowerCase() === 'blah';}))

Array#some() returns true if for any element the callback returns true.

Your String is probably resides in index 0 of an Array.

Appling toLowerCase() on the 0 index should solve your issue:

pathArray[0].toLowerCase()

I was searching for a solution to the error "toLowerCase is not a function"

My issue was solved by adding .toString().toLowerCase() to the variable and that solved the issue

发布评论

评论列表(0)

  1. 暂无评论