Javascript.indexOf not working with int array

the 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 name values ​​are in the array, but for some reason indexOf () never finds them. What?

+8
javascript indexof
source share
1 answer

Try this instead:

 ... var index = js_array.indexOf(parseInt(name, 10)); // so that it does not try to compare strings... ... 
+23
source share

All Articles