19

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);
9
  • 2
    you posted a questions about this only a few minutes ago..which included two code samples.. Commented Jul 1, 2009 at 21:19
  • What does allProductIDs.indexOf(lastProductID) return? Commented Jul 1, 2009 at 21:22
  • 5
    yea, and that's pertaining to something totally different. Why I was getting -1 for indexOf. You suggested to use inArray. So this is a separate issue becuase I cannot figure out why when using inArray I get -1. So this is why I seprated it out and also so I can show code here as if I replied with code in a comment it looks like sh** and is unformatted! So this is not really the same issue! Commented Jul 1, 2009 at 21:26
  • -1, and that was my first problem in another post. You can't supposedly use indexOf on an integer array. Commented Jul 1, 2009 at 21:26
  • @coffee: you can, just not on IE. Commented Jul 1, 2009 at 21:28

6 Answers 6

45

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...

Sign up to request clarification or add additional context in comments.

3 Comments

i have the same problem and this solved it to me .. thanks iim
Thanks mate, this helped me. It seems that 4 was a string for me.
var data=["2","4"]; $.inArray(String(4),data); if you have a haystack of strings (array) and a needle as int. String()
9

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.

4 Comments

I am not familiar with === or this technique. So you pass in the param on the left into the function as n . Then return n and it does some sort of lookup for lastProductID?
yea, an array isn't what I want returned. Nice function though. I resolved my problem, see above.
This worked better for me than the @iim.hlk's answer because my "lastProductID" could be an alphabetic string.
+1, Wow.. thanks... Its really awesome... It is also working with date calculations.
6

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.

4 Comments

You might as well give yourself the correct answer. You've earned it. :-) stackoverflow.com/questions/209329/…
Of course if it was a strongly typed language, your code wouldn't compile.
to be honest, that's what can happen when you use a variable for more than one purpose (and on top, more than one data type) :)
If I have a strongly typed language, I can rely on the datatype. If in JavaScript utilizing jQuery I call val() on an input field I would not want to rely on the type. To my surprise mostly string although I require numeric.
3

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 matches value, $.inArray() returns 0.

According to: http://api.jquery.com/jQuery.inArray/

Comments

0

Are you using any other JavaScript libraries in your page?

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

4 Comments

I am using a mix of standard JavaScript and also some jQuery. I'm using some functions of JCarousel
>>There could be a conflict for the $ shorthand if you are. not the issue...I had this same problem even when I was using indexOf
Using that self-executing anonymous function inside my existing function ended up hiding the lastProductIndex variable so I ended up with an error saying lastProductIndex variable did not exist on the lines after this that were trying to reference it
You would need to declare lastProductIndex outside of the function and assign it a value inside.
-1

This confused me too at first, I had an identical problem. It seems jQuery.inArray() requires a string. So you could just do:

var lastProductID = "6758";

In my case I was trying to do:

var foo = date.getTime()/1000;

Which always resulted in -1 being returned from $.inArray()

But you can just cast it to a string:

var foo = String(date.getTime()/1000);

Hope that helps somebody :)

2 Comments

$.inArray does not require a string. The OPs code works fine - jsbin.com/uqiba .
Check the source of $.inArray - it just runs through and does === comparisons. Strings unrelated, unless you're trying to compare 4 to '4'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.