I cannot figure out why I keep getting -1 for lastProductIndex when clearly the lastProductID is in the array!
var lastProductID = 6758;
var allProductIDs = [5410, 8362, 6638, 6758, 7795, 5775, 1004, 1008, 1013]
var lastProductIndex = $.inArray(lastProductID, allProductIDs);
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...
Try this instead:
$.grep(allProductIDs, function(n) { return n == lastProductID; });
Caveat: grep returns an array.
It looks like jQuery does an === instead of == with inArray.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With