tags:

views:

2159

answers:

7

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

What does allProductIDs.indexOf(lastProductID) return?

Chetan Sastry
Can you do that in IE?
Nosredna
@Nosredna: no, you can't.
Paolo Bergantino
-1, and that was my first problem in another post. You can't supposedly use indexOf on an integer array.
CoffeeAddict
I wish you could. :-)
Nosredna
@coffee: you can, just not on IE.
Paolo Bergantino
@coffeeaddict: I just to meant to run this as a diagnostic in Firebug, not as a replacement to $.inArray in production code.
Chetan Sastry
I am told you CAN use indexOf on arrays with numbers
CoffeeAddict
>>I just to meant to run this as a diagnostic in Firebug (I am not getting you here, what is that? I use firebug but what are you referring to, just the jscript debugger?)
CoffeeAddict
+1  A: 

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);
Russ Cam
I am using a mix of standard JavaScript and also some jQuery. I'm using some functions of JCarousel
CoffeeAddict
>>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
CoffeeAddict
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
CoffeeAddict
You would need to declare lastProductIndex outside of the function and assign it a value inside.
Russ Cam
A: 

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.

CoffeeAddict
You might as well give yourself the correct answer. You've earned it. :-) http://stackoverflow.com/questions/209329/stackoverflow-should-i-answer-my-own-question-or-not
Nosredna
everyone? I love Javascript. It's not Javascript's fault you don't know how it behaves.
Paolo Bergantino
Of course if it was a strongly typed language, your code wouldn't compile.
garrow
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) :)
Russ Cam
A: 

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

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

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.

Joe Chung
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?
CoffeeAddict
yea, an array isn't what I want returned. Nice function though. I resolved my problem, see above.
CoffeeAddict
+4  A: 

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() to convert it to a number...

iim.hlk
i have the same problem and this solved it to me .. thanks iim
Space Cracker
Thanks mate, this helped me. It seems that 4 was a string for me.
Beck
A: 

Is it the missing semicolon at the end of the 2nd line?

anupama