jQuery inArray 总是返回 -1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1071441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery inArray is always returning -1
提问by PositiveGuy
I cannot figure out why I keep getting -1 for lastProductIndex when clearly the lastProductID is in the array!
我不明白为什么当 lastProductID 显然在数组中时,为什么我一直为 lastProductIndex 获取 -1 !
var lastProductID = 6758;
var allProductIDs = [5410, 8362, 6638, 6758, 7795, 5775, 1004, 1008, 1013]
var lastProductIndex = $.inArray(lastProductID, allProductIDs);
回答by AgelessEssence
I had the same problem yesterday,,
我昨天遇到了同样的问题,
var data=[2,4,6,8];
alert( $.inArray(4,data) ) // output 1 as expected
alert( $.inArray("4",data) ) // output -1 as expected
Generally this occurs when you get the value to check from a input element or something that returns a string. You need to do parseInt(string,radix)
to convert it to a number...
通常,当您从输入元素或返回字符串的内容中获取要检查的值时,就会发生这种情况。您需要parseInt(string,radix)
将其转换为数字...
回答by Joe Chung
Try this instead:
试试这个:
$.grep(allProductIDs, function(n) { return n == lastProductID; });
Caveat: grep returns an array.
警告:grep 返回一个数组。
It looks like jQuery does an === instead of == with inArray.
看起来 jQuery 对 inArray 执行了 === 而不是 ==。
回答by PositiveGuy
Here is why everyone hates languages that are not TYPED. I had initially set the lastProductIndex value with a value, but it was a string (because I had gotten the value from an HttpResponse object from returned JSON. So consequently I had set the variable to a string because of the returned JSON value was a string. When I hard coded the number 6758 into $.inArray it worked fine so that caught my attention.
这就是为什么每个人都讨厌非 TYPED 语言的原因。我最初用一个值设置了 lastProductIndex 值,但它是一个字符串(因为我从返回的 JSON 的 HttpResponse 对象中获取了该值。因此,由于返回的 JSON 值是一个字符串,因此我将变量设置为一个字符串. 当我将数字 6758 硬编码到 $.inArray 中时,它运行良好,引起了我的注意。
回答by Youssef
The
$.inArray()
method is similar to JavaScript's native.indexOf()
method in that it returns -1 when it doesn't find a match. If the first element within the array matchesvalue
,$.inArray()
returns 0.
该
$.inArray()
方法类似于 JavaScript 的本机.indexOf()
方法,因为它在找不到匹配项时返回 -1。如果数组中的第一个元素匹配value
,则$.inArray()
返回 0。
According to: http://api.jquery.com/jQuery.inArray/
回答by Russ Cam
Are you using any other JavaScript libraries in your page?
您是否在页面中使用任何其他 JavaScript 库?
There could be a conflict for the $
shorthand if you are. This can be solved in a number of ways, one of which would be wrapping the code in a self-executing anonymous function
$
如果您是,则简写可能会发生冲突。这可以通过多种方式解决,其中一种方法是将代码包装在自动执行的匿名函数中
(function($) {
var lastProductIndex = $.inArray(lastProductID, allProductIDs);
})(jQuery);
回答by firecall
This confused me too at first, I had an identical problem. It seems jQuery.inArray() requires a string. So you could just do:
起初这也让我感到困惑,我遇到了同样的问题。似乎 jQuery.inArray() 需要一个字符串。所以你可以这样做:
var lastProductID = "6758";
var lastProductID = "6758";
In my case I was trying to do:
就我而言,我试图这样做:
var foo = date.getTime()/1000;
var foo = date.getTime()/1000;
Which always resulted in -1 being returned from $.inArray()
这总是导致 $.inArray() 返回 -1
But you can just cast it to a string:
但是您可以将其强制转换为字符串:
var foo = String(date.getTime()/1000);
var foo = String(date.getTime()/1000);
Hope that helps somebody :)
希望对某人有所帮助:)