6

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.

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!

2
  • Show us the code for your function so we can advise how to make it case insensitive. Commented Nov 9, 2011 at 22:42
  • Thanks guys, Sorted it! Thanks! Commented Nov 9, 2011 at 23:06

9 Answers 9

21

Easiest way is to JOIN the mixed-case array into a string, lowercase it, then SPLIT the string back into an array.

Examples:

var tmp = mcArray.join('~').toLowerCase()
var lcArray = tmp.split('~')
Sign up to request clarification or add additional context in comments.

2 Comments

What happens if any of the array items contains '~' ?
Pretty silly... obviously that would mess up the logic. This is assuming "~" is a safe character. You could use any other character.
7

You will need to convert the elements of the array to lower case, not the Array object itself. I don't know exactly what happens inside your function, but something like this:

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 example of the above.

1 Comment

Hi, sorry for being so vague. I have posted some code above. Thank you
6

You can't use toLowerCase() on an array, unless you extend it.

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

Comments

3

Arrays do not have a toLowerCase method. You can create one or just call toLowerCase on each string when iterating through the array.

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].toLowerCase().indexOf(searchlow));   

Comments

3

Another way using ES6, with Array.prototype.map and arrow function.

var arr = ['Billie', 'Jean'];
var arr_lower = arr.map(item => item.toLowerCase());
console.log(arr_lower);

Comments

2

you can not call toLowerCase on array object you need to call on individual member of array

Comments

2

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

1 Comment

This works, or maybe you could simply concatenate all three methods into one? var arrayLowerString = array.toString().toLowerCase().split(',')
1
array_name_tolowercase = array_name.map(x=>x.toLowerCase)

Comments

0
function(resp) {
    return this.colors.filter(function(color) {
        return color.toLowerCase() === resp.toLowerCase();
    })[0];
}

Comments

Your Answer

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