尝试从以下数组中仅检索数值时,出现错误“无法读取未定义的属性'toString'”。虽然我看到输出正确,但是出现此错误,代码失败。

 var array=['~ 9    view passages','~ 17    view passages','~ 1    view passages','~ 3    view passages','~ 4    view passages','~ 1    view passages'];
    for (var i=0;i<=array.length;i++){
    var Arraystring=array[i].toString();
    var temp1 = Arraystring.replace("    view passages","");
    var temp2 = temp1.replace("~","");
    var temp3=parseInt(temp2);
    console.log("String converted Array "+Arraystring);
    console.log("Stripped Array "+temp2);
    console.log("Integer Array "+temp3);
    }


错误:
无法读取未定义的属性“ toString”

最佳答案

您应该使用< array.length而不是<= array.length

当前,您正在解析边界值,因此它无法调用toString()函数。

试试这个

var array = ['~ 9    view passages', '~ 17    view passages', '~ 1    view passages', '~ 3    view passages', '~ 4    view passages', '~ 1    view passages'];
for (var i = 0; i < array.length; i++) {
    var Arraystring = array[i].toString();
    var temp1 = Arraystring.replace("    view passages", "");
    var temp2 = temp1.replace("~", "");
    var temp3 = parseInt(temp2);
    console.log("String converted Array " + Arraystring);
    console.log("Stripped Array " + temp2);
    console.log("Integer Array " + temp3);
}

关于javascript - 从混合字符数组中仅检索整数值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44300352/

10-12 00:25