Javascript:如何创建长度未知的二维数组?

时间:2014-08-20 09:45:08

标签: javascript arrays multidimensional-array

我试图在javascript中创建2个二维数组作为全局变量,如下所示:

player = [nPlayers];
helps = [nPlayers];

我将它们填入for循环中,如下所示:

for (i = 0; i < nPlayers; i++) {
    j = i + 1;
    name = window.localStorage.getItem('player' + j);

    player[i] = [j, name, 0];                           
    helps[i] = [1,1,1,1];

直到这里,一切都很好,但每次我做这样的事情:

var currentPlayer = 1;
player[currentPlayer][2];

它给了我这个错误:

Uncaught TypeError: Cannot read property '0' of undefined

有人可以帮我吗?

0 个答案:

没有答案