javascript 使用 for 循环而不是 map 以小写形式返回数组中的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16253742/
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
Return all values from array in lowercase using for loop instead of map
提问by Olivera Kovacevic
var sorted = words.map(function(value) {
return value.toLowerCase();
}).sort();
This code returns all values from words array in lowercase and sorts them, but I wanna do the same with a for
loop but I can't.
这段代码以小写形式返回 words 数组中的所有值并对它们进行排序,但我想用for
循环做同样的事情,但我不能。
I tried:
我试过:
for (var i = 0; i < words.length; i++) {
sorted = [];
sorted.push(words[i].toLowerCase());
};
采纳答案by Bergi
With arrays, the +=
operator does not do what you expect - it calls .toString
on the array and concatenates them. Instead, you want to use the array push
method:
对于数组,+=
运算符不会执行您期望的操作 - 它调用.toString
数组并将它们连接起来。相反,您想使用数组push
方法:
var sorted = [];
for (var i = 0; i < words.length; i++) {
sorted.push(words[i].toLowerCase());
}
sorted.sort();
回答by Rory McCrossan
You can also now achieve this very simply by using an arrow function and the map()
method of the Array:
您现在还可以通过使用箭头函数和map()
Array的方法非常简单地实现这一点:
var words = ['Foo','Bar','Fizz','Buzz'].map(v => v.toLowerCase());
console.log(words);
Note that map()
will only work in browsers which support ES2015. In other words, anything except IE8 and lower.
请注意,它map()
仅适用于支持 ES2015 的浏览器。换句话说,除了 IE8 及更低版本之外的任何东西。
Similarly, arrow functions will not work at all in IE. For a legacy browser safe version you would need to use an anonymous function:
同样,箭头函数在 IE 中根本不起作用。对于旧版浏览器安全版本,您需要使用匿名函数:
var words = ['Foo','Bar','Fizz','Buzz'].map(function(v) {
return v.toLowerCase();
});
console.log(words);
回答by kennebec
push is overused.
推被过度使用。
for (var i = 0, L=words.length ; i < L; i++) {
sorted[i]=words[i].toLowerCase();
}
If you want fastand have a very large array of words, call toLowerCase once-
如果您想要快速且有大量单词,请调用 toLowerCase 一次-
sorted=words.join('|').toLowerCase().split('|');
回答by sandrina-p
I know this is a later answer but I've found a prettier and simpler way:
我知道这是后来的答案,但我找到了一种更漂亮、更简单的方法:
yourArray = ['this', 'iS an', 'arrAy'];
console.log(yourArray); // ["this", "iS an", "arrAy"]
yourLowerArray = yourArray.toLocaleString().toLowerCase().split(',');
console.log(yourLowerArray); //["this", "is an", "array"]
Explaining what this does:
解释它的作用:
.toLocaleString()
-> transform the array into a string separated by commas.
.toLocaleString()
-> 将数组转换为逗号分隔的字符串。
.toLowerCase()
-> convert that string to lower case.
.toLowerCase()
-> 将该字符串转换为小写。
.split(',')
-> convert the string in lower case back to an array.
.split(',')
-> 将小写字符串转换回数组。
Hope this helps you!
希望这对你有帮助!
回答by monkeyinsight
toLowerCase()
is function, you should write ()
after it
toLowerCase()
是函数,你应该()
在它后面写
回答by DaveB
The toLowerCase method is not being called in your code, only referenced. Change your line in the loop to:
toLowerCase 方法没有在您的代码中被调用,只是被引用。将循环中的行更改为:
sorted += words[i].toLowerCase();
Add () to call the method.
添加()来调用方法。
Complete working code:
完整的工作代码:
var words = ["FOO", "BAR"];
var sorted = [];
for (var i = 0; i < words.length; i++) {
sorted.push(words[i].toLowerCase());
};
console.log(sorted);
回答by ljfranklin
I'm assuming you're declaring sorted
as an array. If so use the push
method rather than +=
:
我假设你声明sorted
为一个数组。如果是这样,请使用该push
方法而不是+=
:
for (var i = 0; i < words.length; i++) {
sorted.push(words[i].toLowerCase());
}