Javascript .indexOf无法处理int数组

时间:2013-01-29 20:56:19

标签: javascript indexof

代码:

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 />" )
    ...
}

输出:

301
300,299,301,290,303,304,302,310,291,306,308,305,307,292,294,295,309
-1
300

name的所有可能值都在数组中,但由于某种原因,indexOf()永远不会找到它们。怎么了?

1 个答案:

答案 0 :(得分:26)

请改为尝试:

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