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

I tried:

for (var i = 0; i < words.length; i++) {
  sorted = [];
  sorted.push(words[i].toLowerCase());
};
1
  • 4
    A suggestion for future reference: When you update the code in a question as a result of one of the answers, please don't edit the original (incorrect) code. Instead, keep that original code unchanged and add a new section to the end of the question like, "I modified the code as suggested in XYZ's answer, but it still doesn't work. Here's the new code: ..." This way the question and answers will make much more sense for future visitors. Apr 27, 2013 at 17:57

8 Answers 8

74

You can also now achieve this very simply by using an arrow function and the map() method of the 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.

Similarly, arrow functions will not work at all in IE. For a legacy browser safe version you would need to use an anonymous function:

var words = ['Foo','Bar','Fizz','Buzz'].map(function(v) {
  return v.toLowerCase();
});
console.log(words);

2
  • 1
    And if you want to use that as a function, use function arrayToLowerCase(array) { return array.map(function(v) { return v.toLowerCase(); }); } Aug 18, 2018 at 18:55
  • 1
    Array.prototype.toLowerCase = function(){return this.map(v => v.toLowerCase())} Sep 18, 2018 at 11:12
46

push is overused.

for (var i = 0, L=words.length ; i < L; i++) {
  sorted[i]=words[i].toLowerCase();
}

If you want fast and have a very large array of words, call toLowerCase once-

sorted=words.join('|').toLowerCase().split('|');
0
16

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:

var sorted = [];
for (var i = 0; i < words.length; i++) {
    sorted.push(words[i].toLowerCase());
}
sorted.sort();
3
  • I updated the code a bit (please see), but now it returns only 1 element, not all of them. Apr 27, 2013 at 15:31
  • 3
    You have your sorted = []; inside the loop - where it overwrites the array with a new one repeatedly (and only the last one, with the last item, will be returned). See my answer for how the code should look.
    – Bergi
    Apr 27, 2013 at 15:45
  • Yea, I compared your code and mine and realized that was the problem. Thanks! Apr 27, 2013 at 16:08
9

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.

.toLowerCase() -> convert that string to lower case.

.split(',') -> convert the string in lower case back to an array.

Hope this helps you!

1
  • 2
    note that toLocaleString() might not always return a comma separated string Oct 16, 2018 at 10:38
0

toLowerCase() is function, you should write () after it

0
0

I'm assuming you're declaring sorted as an array. If so use the push method rather than +=:

for (var i = 0; i < words.length; i++) {
  sorted.push(words[i].toLowerCase());
}
0

The toLowerCase method is not being called in your code, only referenced. Change your line in the loop to:

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);
0
words.forEach(function (item, index) {
    words[index] = item.toLowerCase();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.