js中的未定义参数

sou*_*942 0 javascript arrays undefined

我收到一个类型错误,指出每当我将输入添加到html页面时,数组testA [i]是未定义的.我有数组,我正在尝试使用push方法将数字值添加到数组中,以添加到数组的第二部分,即([0] [货币])

function Test() {

var testA = [];
for (i = 0; i < 4; i++) {
        this.currency = prompt("Please enter a 3-letter currency abbreviation", "");
        testA[i].push(currency);
        }
}
var index = new Test();
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

任何关于为什么数组未定义的帮助将不胜感激.

注意:我现在尝试了testA.push(货币)和testA [i] = this.currency,我仍然得到与以前相同的错误.

注意:最终版本应该循环通过4个不同的问题并且每次将它们添加到数组中.在循环结束时,应该创建一个新的数组变体,并将输入的新数据集添加到其中.就像是 for(i = 0; i < 4; i++) { testA[i] = i; for(j = 0; j < 4; j++) { this.currency = prompt("Please enter a 3-letter currency abbreviation", ""); testA[i][j] = this.currency; } }

但是在这个时候我只是想让它发挥作用.

Lar*_*son 5

您不在push索引上使用该方法.您在阵列本身上使用它.

替换它

testA[i].push(currency);
Run Code Online (Sandbox Code Playgroud)

有了这个

testA.push(currency);
Run Code Online (Sandbox Code Playgroud)