Javascript .indexOf not working on an int array

杀马特。学长 韩版系。学妹 提交于 2019-12-10 12:46:07

问题


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?


回答1:


Try this instead:

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


来源:https://stackoverflow.com/questions/14592115/javascript-indexof-not-working-on-an-int-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!