难以将字符串数组转换为LowerCase 并添加到我的函数中。Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8072774/
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
Difficulty converting an Array of strings toLowerCase and adding to my function. Javascript
提问by Tony
I have a function that searches through an array of strings. It works perfectly, except for the fact that its case sensitive. I need it to be case insensitive.
我有一个搜索字符串数组的函数。它工作得很好,除了它区分大小写的事实。我需要它不区分大小写。
The easiest way would be to make the function convert the array to lowercase, so when the function performs a search through the array, it is able to find it, regardless of its case.
最简单的方法是让函数将数组转换为小写,这样当函数在数组中执行搜索时,无论大小写,它都能找到它。
I have tried adding var array_name_tolowercase = array_name.toLowerCase();
within my function so that when the function is called, it can work on all arrays; if needed.
我尝试var array_name_tolowercase = array_name.toLowerCase();
在我的函数中添加,以便在调用该函数时,它可以在所有数组上工作;如果需要的话。
Im sorry I haven't made myself clear at all, if this is the case, please let me know and I will try my best to re-explain. Thanks in advance!
对不起,我完全没有说清楚,如果是这种情况,请告诉我,我会尽力重新解释。提前致谢!
采纳答案by James Allardice
You will need to convert the elementsof the array to lower case, not the Array object itself. I don't know exactly what happens inside your function, but something like this:
您需要将数组的元素转换为小写,而不是 Array 对象本身。我不知道你的函数内部到底发生了什么,但像这样:
for(var i = 0; i < arrayName.length; i++) {
if(arrayName[i].toLowerCase() === "whatever") {
//Found a match!
}
}
Post some more of your code and it will be easier to provide a more precise solution! Here's an exampleof the above.
发布更多您的代码,提供更精确的解决方案会更容易!这是上面的一个例子。
回答by Tony
Easiest way is to JOIN the mixed-case array into a string, lowercase it, then SPLIT the string back into an array.
最简单的方法是将大小写混合的数组 JOIN 成一个字符串,将其小写,然后将字符串拆分回数组中。
Examples:
例子:
var tmp = mcArray.join('~').toLowerCase()
var lcArray = tmp.split('~')
回答by rkw
You can't use toLowerCase() on an array, unless you extend it.
你不能在数组上使用 toLowerCase() ,除非你扩展它。
Put this somewhere in your code, then from here on out, you can use it on arrays (note, only if your array is filled with strings)
把它放在你的代码中,然后从这里开始,你可以在数组上使用它(注意,只有当你的数组用字符串填充时)
Array.prototype.toLowerCase = function() {
for (var i = 0; i < this.length; i++) {
this[i] = this[i].toString().toLowerCase();
}
}
回答by Will
Arrays do not have a toLowerCase
method. You can create one or just call toLowerCase on each string when iterating through the array.
数组没有toLowerCase
方法。在遍历数组时,您可以在每个字符串上创建一个或仅调用 toLowerCase。
Array.prototype.toLowerCase = function() {
var i = this.length;
while ( --i >= 0 ) {
if ( typeof this[i] === "string" ) {
this[i] = this[i].toLowerCase();
}
}
return this;
};
For the code you posted, you can change var a = (array[i].indexOf(searchlow));
to
对于您发布的代码,您可以更改var a = (array[i].indexOf(searchlow));
为
var a = (array[i].toLowerCase().indexOf(searchlow));
回答by Amritpal Singh
you can not call toLowerCase on array object you need to call on individual member of array
你不能在数组对象上调用 toLowerCase 你需要调用数组的单个成员
回答by Martie Henry
Personally, I find it much easier to do it this way:
就我个人而言,我发现这样做要容易得多:
array = ["Apples","Oranges","FRUIT"];
console.log("array: "+array); //array: Apples,Oranges,FRUIT
arrayString = array.toString();
arrayLowerString = arrayString.toLowerCase();
arrayLower = arrayLowerString.split(",");
console.log("arrayLower: "+arrayLower); //apples,oranges,fruit
回答by Sumit
Another way using ES6, with Array.prototype.map
and arrow function.
使用ES6,用另一种方式Array.prototype.map
和箭头的功能。
var arr = ['Billie', 'Jean'];
var arr_lower = arr.map(item => item.toLowerCase());
console.log(arr_lower);
回答by Quimby
array_name_tolowercase = array_name.map(x=>x.toLowerCase)
回答by minki
function(resp) {
return this.colors.filter(function(color) {
return color.toLowerCase() === resp.toLowerCase();
})[0];
}