1

I'm trying to make all array indexes lowercase strings, but it's not working. I looked at other answers on here and tried their solutions like using toString() before adding toLowerCase but it doesn't work, which is weird.

I created a jsfiddle of the problem here.

JS:

$(colorArr).each(function(i, item) // loop thru each of elements in colorArr and make lowercase + trim
{
    if(colorArr[i] !== undefined) // check if colorArr index undefined
      {
      colorArr[i].toString().toLowerCase().trim(); // FIX HERE
      /* TRIED - DIDN'T WORK!
      colorArr[i].toLowerCase().trim();
      */
       }
});
TheAmazingKnight
  • 2,442
  • 9
  • 49
  • 77
  • Possible duplicate of [why does the trim doesn't really trim? trim not working in my case](http://stackoverflow.com/questions/18685076/why-does-the-trim-doesnt-really-trim-trim-not-working-in-my-case) – JJJ Mar 23 '16 at 14:03
  • @juhana This is has nothing to do with `trim()` even though it's in the code, I'm asking only about `toLowerCase()`. – TheAmazingKnight Mar 23 '16 at 14:04
  • The solution is the same: you're applying toLowerCase and trim but not doing anything with the result. – JJJ Mar 23 '16 at 14:05

3 Answers3

4

i updated your fiddle

https://jsfiddle.net/af91r2cq/6/

colorArr[i] = colorArr[i].toString().toLowerCase().trim(); // FIX HERE

your way was really close ;)

messerbill
  • 5,499
  • 1
  • 27
  • 38
  • Comparing the other variable `checkedAttr`, why did it work for `checkedAttr` instead of `colorArr` when both are array of strings? – TheAmazingKnight Mar 23 '16 at 14:08
2

You need to set the value back

It should be

colorArr[i] = colorArr[i].toString().toLowerCase().trim(); // FIX HERE

Or simply

colorArr = colorArr.map(function(value){ return value ? value.toLowerCase().trim() : ""; });
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

Another way change all defined values in the array to lowercase is to use the jQuery.map function like so:

colorArr = $.map(colorArr, function(item, i) {
  if(item !== undefined) {
    return item.toString().toLowerCase().trim();
  }
});
Gary S.
  • 146
  • 5