【问题标题】:Any way to make jQuery.inArray() case insensitive?有什么方法可以使 jQuery.inArray() 不区分大小写?
【发布时间】:2010-08-02 19:06:30
【问题描述】:

标题总结了它。

【问题讨论】:

    标签: jquery arrays


    【解决方案1】:

    如果有人想要使用 的更集成的方法:

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

    【讨论】:

    • 这应该添加到jQuery API,每次复制粘贴都很难,无论如何都可以
    • 这太棒了。它只需要在最后一行的开头有一个右花括号。
    • 为什么是$.extend({ inArrayIn: function ... 而不仅仅是$.inArrayIn = function ...
    • @Luke:除了它的一种方法之外没有什么特别的原因。
    【解决方案2】:

    你可以使用each()...

    // Iterate over an array of strings, select the first elements that 
    // equalsIgnoreCase the 'matchString' value
    var matchString = "MATCHME".toLowerCase();
    var rslt = null;
    $.each(['foo', 'bar', 'matchme'], function(index, value) { 
      if (rslt == null && value.toLowerCase() === matchString) {
        rslt = index;
        return false;
      }
    });
    

    【讨论】:

    • 你会想要添加一个“return false;”在 if 语句的末尾,因此在找到匹配元素后“每个”都不会继续。 (在 jQuery.each() 中,“return false;”等价于常规 JavaScript 循环中的“break;”。)
    • 不是toLowerCase而不是toLower吗?
    • 技术上,如果未找到 InArray(),则返回 -1。
    • 在循环之前将matchString.toLowerCase()值存储在变量中而不是为每次迭代计算它不是更有效吗?
    • 我不认为equals 是 JavaScript 中的原生方法?我觉得应该是===吧?
    【解决方案3】:

    感谢@Drew Wills。

    我改写成这样:

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

    【讨论】:

      【解决方案4】:

      没有。您将不得不摆弄您的数据,我通常将所有字符串都设为小写以便于比较。还有可能使用自定义比较函数,该函数会进行必要的转换以使比较不区分大小写。

      【讨论】:

        【解决方案5】:

        可以循环遍历数组并toLower每个元素并toLower您要查找的内容,但是在那个时候,您最好只比较它而不是使用inArray()

        【讨论】:

          【解决方案6】:

          这些天我更喜欢使用underscore 来完成这样的任务:

          a = ["Foo","Foo","Bar","Foo"];
          
          var caseInsensitiveStringInArray = function(arr, val) {
              return _.contains(_.map(arr,function(v){
                  return v.toLowerCase();
              }) , val.toLowerCase());
          }
          
          caseInsensitiveStringInArray(a, "BAR"); // true
          

          【讨论】:

            【解决方案7】:

            看起来您可能必须为此实施自己的解决方案。 Here 是一篇关于向 jQuery 添加自定义函数的好文章。您只需要编写一个自定义函数来循环和规范化数据然后进行比较。

            【讨论】:

              【解决方案8】:

              这种方式对我有用..

              var sColumnName = "Some case sensitive Text"
              
              if ($.inArray(sColumnName.toUpperCase(), getFixedDeTasksColumns().map((e) => 
              e.toUpperCase())) == -1) {// do something}
              

              【讨论】:

              • 这对我很有用,我认为这应该是公认的答案!
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多