Any way to make jQuery.inArray() case insensitive?

后端 未结 8 1662
轮回少年
轮回少年 2020-12-03 16:44

Title sums it up.

8条回答
  •  盖世英雄少女心
    2020-12-03 17:10

    Thank you to @Drew Wills.

    I rewrote it as this:

    function inArrayCaseInsensitive(needle, haystackArray){
        //Iterates over an array of items to return the index of the first item that matches the provided val ('needle') in a case-insensitive way.  Returns -1 if no match found.
        var defaultResult = -1;
        var result = defaultResult;
        $.each(haystackArray, function(index, value) { 
            if (result == defaultResult && value.toLowerCase() == needle.toLowerCase()) {
                result = index;
            }
        });
        return result;
    }
    

提交回复
热议问题