有没有办法使jQuery.inArray()不区分大小写?
标题概括了这一点。不。您将不得不修改数据,为了便于比较,我通常将所有字符串都改为小写。还可以使用自定义比较函数,该函数将进行必要的转换,使比较不区分大小写。可以循环遍历数组,降低每个元素的大小写,降低所需的大小写,但此时,您也可以对其进行比较,而不是使用inArray()您可以使用)有没有办法使jQuery.inArray()不区分大小写?,jquery,arrays,Jquery,Arrays,标题概括了这一点。不。您将不得不修改数据,为了便于比较,我通常将所有字符串都改为小写。还可以使用自定义比较函数,该函数将进行必要的转换,使比较不区分大小写。可以循环遍历数组,降低每个元素的大小写,降低所需的大小写,但此时,您也可以对其进行比较,而不是使用inArray()您可以使用) 看起来您可能需要实现自己的解决方案。这是一篇关于向jQuery添加自定义函数的好文章。您只需编写一个自定义函数来循环和规范化数据,然后进行比较。感谢@Drew Wills 我将其改写为: function inAr
看起来您可能需要实现自己的解决方案。这是一篇关于向jQuery添加自定义函数的好文章。您只需编写一个自定义函数来循环和规范化数据,然后进行比较。感谢@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;
}
这些天来,我更喜欢使用以下任务:
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
如果有人希望采用更综合的方法,请使用:
(函数($){
$扩展({
//大小写无关$.inArray(http://api.jquery.com/jquery.inarray/)
//$.inarayin(值,数组[,fromIndex])
//值(类型:字符串)
//要搜索的值
//数组(类型:数组)
//用于搜索的数组。
//fromIndex(类型:编号)
//开始搜索的数组的索引。
//默认值为0,它将搜索整个数组。
inarayin:功能(元素、arr、i){
//无论如何,不要寻找字符串,请使用默认方法
如果(元素类型!=='string'){
返回$.inArray.apply(这是参数);
}
//确认已填充阵列
如果(arr){
var len=阵列长度;
i=i?(i<0?数学最大值(0,len+i):i:0;
elem=elem.toLowerCase();
对于(;i
这种方法对我很有效
var sColumnName = "Some case sensitive Text"
if ($.inArray(sColumnName.toUpperCase(), getFixedDeTasksColumns().map((e) =>
e.toUpperCase())) == -1) {// do something}
您需要在if语句的末尾添加一个“return false;”,以便在找到匹配元素后“each”不会继续。(在jQuery.each()中,“return false;”相当于常规JavaScript循环中的“break;”)它不是toLowerCase而不是toLower吗?从技术上讲,如果找不到,InArray()返回-1。存储
matchString.toLowerCase()不是更有效吗
循环前变量中的值,而不是每次迭代都计算它?我不认为等于
是JavaScript中的本机方法?我认为这应该是==
,对吗?这应该添加到jQuery API中,每次复制粘贴都会很困难,很好,不管怎样这太棒了。它只需要在最后一行的开头有一个右大括号。为什么要使用$.extend({inarayin:function…
),而不仅仅是$。inarayin=function…
?@Luke:除了它的一种方法之外,没有什么特别的原因。
(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);
var sColumnName = "Some case sensitive Text"
if ($.inArray(sColumnName.toUpperCase(), getFixedDeTasksColumns().map((e) =>
e.toUpperCase())) == -1) {// do something}