从嵌套数组打印Javascript

Javascript printing from nested arrays

本文关键字:Javascript 打印 数组 嵌套      更新时间:2023-09-26

我目前正在开发一个随机数生成器脚本,该脚本将生成三个数字。前两个数字将用于通过引用行和列来选择"表"中的"单元格"。每个单元格中有两个数字可供选择。然后,第三个数字将决定要选择两个数字中的哪一个。我已经理解了大部分内容,但最后一部分我有问题。

我的"解决方案"是创建二十个数组,每个数组中又有二十个数组。以下是第一个阵列的示例:

在标题中,我有以下内容:

生成前两个数字的脚本:

var randOne20 = Math.floor(Math.random()*20);
var randTwo20 = Math.floor(Math.random()*20);

这是20个阵列中的第一行:

var lvl1Roll1 = [[ 1,0 ],[ 1,1 ],[ 1,2 ],[ 1,3 ],[ 1,4 ],[ 1,5 ],[ 1,6 ],[ 1,7 ],[ 1,8 ],[ 1,9 ],[ 1,10 ],[ 1,11 ],[ 1,12 ],[ 1,13 ],[ 1,14 ],[ 1,15 ],[ 1,16 ],[ 1,17 ],[ 1,18 ],[ 1,19 ]];

第二行是"lvl1Roll2",第三行是"lvl1Roll3"等等,一直到"lvl1Loll20"

在正文中,我有以下脚本:

var randOne = randOne20 + 1;
document.write(window['lvl1Roll'+randOne][randTwo20]);

randOne变量用于选择适当的行。

我可以想出如何选择一个特定的单元格(使用randTwo20变量),但我不知道如何选择每个单元格中的第一个或第二个数字。

现在,为了澄清,我还没有列出第三个随机数生成器代码,因为目前我只是想知道如何在每个单元格中选择第一个或第二个数字。一旦我弄清楚了,我就可以使用if/else语句了。

此外,如果我不想打印出数字,而是选择它作为变量,我该怎么做?

感谢您的帮助!

保重,度过美好的一天。。。。

ciao,约翰。

我真的不确定你想做什么,但是。。。获取数组的第二个元素中的第一个元素就像arr[0]arr[1]一样简单,如下所示:

// First:
document.write(window['lvl1Roll'+randOne][randTwo20][0]);
// Second:
document.write(window['lvl1Roll'+randOne][randTwo20][1]);

要随机化,只需遵循与以前相同的模式,但使用Math.round而不是Math.floor,并且不要乘以20:

var randOne20 = Math.floor(Math.random()*20);
var randTwo20 = Math.floor(Math.random()*20);
var randOne = Math.round(Math.random());
document.write(window['lvl1Roll'+randOne20][randTwo20][randOne]);

不过,还有一件事:您不需要将不同的行放在单独的变量中。你可以把它做成一个大阵列:

var lvl1Roll = [
    [
        [ 1,0 ], [ 1,1 ], [ 1,2 ], [ 1,3 ], [ 1,4 ], [ 1,5 ], [ 1,6 ], [ 1,7 ], [ 1,8 ], [ 1,9 ], [ 1,10 ], [ 1,11 ], [ 1,12 ], [ 1,13 ], [ 1,14 ], [ 1,15 ], [ 1,16 ], [ 1,17 ], [ 1,18 ], [ 1,19 ]
    ],
    // [ lvl1Roll2 ],
    // [ lvl1Roll3 ], etc.
];

然后使用它从表中选择一个值,并将其放入变量result:中

var result = lvl1Roll[randOne20][randTwo20][randOne];

最后:我怀疑,如果你告诉我们"滚动"背后的逻辑,你根本不需要这个"桌子"是很有道理的。我可能错了,但是。。。是否值得再发布一个问题?只是一个想法。

如果你想在两个数字之间随机选择(假设0和1),那么:

var i = Math.random() < 0.5? 0 : 1;

var i = Math.random()*2 | 0;

应该做这项工作。因此:

document.write(window['lvl1Roll'+randOne][randTwo20][i]);

document.write(window['lvl1Roll'+randOne][randTwo20][Math.random() < 0.5? 0 : 1]);

document.write(window['lvl1Roll'+randOne][randTwo20][Math.random()*2 | 0);

你想要多少?:-)

如果我理解正确,我认为最简单的方法是使用一个包含多个数组的数组。第一个是table数组,table数组中的每个条目是rowrow中的每个条目的是cell。这将使引用表中的任何单元格变得非常容易。下面是一个3x3的例子:

var table = [
    [0,1,2],
    [3,4,5],
    [6,7,8],
];
table[0][0]; // First Row, First Column, Equals 0;
table[1][1]; // Second Row, Second Column, Equals 4;
table[2][2]; // Third Row, Third Column, Equals 8;

如果你愿意的话,你实际上可以把你的20个数组推到一个数组中来创建表:

var table = [];
table.push(randOne20);
table.push(randTwo20);
etc...

如果你想用随机数自动加载数组,你可以很容易地用循环来完成。

var table = [];
for (var i = 0; i < 20; i++){ // Create 20 rows
    var row = [];
    for (var x=0; i<20; i++){ // Create 20 cells;
        var cell = [Math.round(Math.random), Math.round(Math.random)]; // Adds a two item array of random 0's or 1's
        row.push(cell); // Adds the cell to the row
    }
    table.push(row); // Adds the row to table
}