Javascript .indexOf not working on an int array
23,720
Try this instead:
...
var index = js_array.indexOf(parseInt(name, 10)); // so that it does not try to compare strings...
...
Author by
gta0004
Updated on August 20, 2022Comments
-
gta0004 5 months
Code:
function showlayer(name){ var size = js_array.length var index = js_array.indexOf(name); var plusOne = js_array[index+1]; document.write("" + name + "<br />" + js_array + "<br />" + index + "<br />" + plusOne + "<br />" ) ... }
Output:
301 300,299,301,290,303,304,302,310,291,306,308,305,307,292,294,295,309 -1 300
All possible values of name are in the array, but for some reason indexOf() never finds them. Whats up?
-
Naftali almost 10 years@gta0004 happy to help ^_^
-
SLaks almost 10 years@gta0004: Apparently,
name
is not an int. -
Naftali almost 10 years@SLaks hehe that is most probably
true
. -
gta0004 almost 10 years@SLaks look at the output. 301 = name. so idk
-
Naftali almost 10 years@gta0004 output
typeof name
and your shall see ^_^ -
cvng over 7 yearsOr, simply cast the key to Int:
array.indexOf(+name)
.