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();
      */
       }
});
3
  • Possible duplicate of why does the trim doesn't really trim? trim not working in my case Commented Mar 23, 2016 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(). Commented Mar 23, 2016 at 14:04
  • The solution is the same: you're applying toLowerCase and trim but not doing anything with the result. Commented Mar 23, 2016 at 14:05

3 Answers 3

4

i updated your fiddle

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

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

your way was really close ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Comparing the other variable checkedAttr, why did it work for checkedAttr instead of colorArr when both are array of strings?
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() : ""; });

Comments

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();
  }
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.