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

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

Title sums it up.

相关标签:
8条回答
  • 2020-12-03 17:32

    In case anyone wanted a more integrated approach using jquery:

    (function($){
        $.extend({
            // Case insensative $.inArray (http://api.jquery.com/jquery.inarray/)
            // $.inArrayIn(value, array [, fromIndex])
            //  value (type: String)
            //    The value to search for
            //  array (type: Array)
            //    An array through which to search.
            //  fromIndex (type: Number)
            //    The index of the array at which to begin the search.
            //    The default is 0, which will search the whole array.
            inArrayIn: function(elem, arr, i){
                // not looking for a string anyways, use default method
                if (typeof elem !== 'string'){
                    return $.inArray.apply(this, arguments);
                }
                // confirm array is populated
                if (arr){
                    var len = arr.length;
                        i = i ? (i < 0 ? Math.max(0, len + i) : i) : 0;
                    elem = elem.toLowerCase();
                    for (; i < len; i++){
                        if (i in arr && arr[i].toLowerCase() == elem){
                            return i;
                        }
                    }
                }
                // stick with inArray/indexOf and return -1 on no match
                return -1;
            }
        });
    })(jQuery);
    
    0 讨论(0)
  • 2020-12-03 17:32

    It looks like you may have to implement your own solution to this. Here is a good article on adding custom functions to jQuery. You will just need to write a custom function to loop and normalize the the data then compare.

    0 讨论(0)
提交回复
热议问题