Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的多维数组递增_Javascript_Multidimensional Array - Fatal编程技术网

JavaScript中的多维数组递增

JavaScript中的多维数组递增,javascript,multidimensional-array,Javascript,Multidimensional Array,我目前正在做一些非常基本的工作,但我有一个困难的时候。我希望在二维数组中获得一个随机索引,并在for循环每次翻转时将其递增一 var dice = [[],[]]; // Get totals. for(var i = 0; i < 30000; i++) { var dieOne = Math.floor(Math.random() * 6 + 1); var dieTwo = Math.floor(Math.random() * 6 + 1); dic

我目前正在做一些非常基本的工作,但我有一个困难的时候。我希望在二维数组中获得一个随机索引,并在for循环每次翻转时将其递增一

var dice = [[],[]];

// Get totals.
for(var i = 0; i < 30000; i++) {
    var dieOne = Math.floor(Math.random() * 6 + 1);
    var dieTwo = Math.floor(Math.random() * 6 + 1);
        dice[[dieOne][dieTwo]]++;
    }

    // All index values equal 30,000 for some reason
    alert(dice[[1][3]]);
var dice=[],[];
//获取总数。
对于(变量i=0;i<30000;i++){
var dieOne=数学地板(数学随机()*6+1);
var dieTwo=Math.floor(Math.random()*6+1);
骰子[[dieOne][dieTwo]]++;
}
//出于某种原因,所有索引值都等于30000
警惕(骰子[[1][3]]);
为什么这个for循环会将所有索引设置为30000?我是否错误地使用了JavaScript数组


谢谢。

你在做什么

你似乎误解了你所做的事情的语法。目前相当于

var dieOne = Math.floor(Math.random() * 6 + 1);
var dieTwo = Math.floor(Math.random() * 6 + 1);
var foo;
foo = dieOne; // a number
foo = [foo];  // an array with one number in it
foo = foo[dieTwo]; // probably undefined, unlikely case of `dieTwo = 0`
                   // which would give back `dieOne`
dice[foo] = dice[foo] + 1; // most likely trying to add 1 to property `undefined`
那以后你要做什么

foo = [1]; // an array length 1
foo = foo[3]; // undefined, it doesn't have an item here
foo = dice[foo]; // = dice[undefined] = undefined
alert(foo); // alerting "undefined"

您可能想做的事情

看起来您实际上想要一个
6
数组,每个
6
数组都是数字;构建你最喜欢的方式

var dice = [
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]
];
那么你的循环是

var dieOne, dieTwo, i;
for(i = 0; i < 30000; ++i) {
    dieOne = Math.floor(Math.random() * 6); // indices start at 0 and
    dieTwo = Math.floor(Math.random() * 6); // 6 of them means max is 5
    dice[dieOne][dieTwo]++; // count the brackets here..
}
这虽然(不幸的)在技术上是有效的JavaScript,但并不能满足您的需要:

dice[[dieOne][dieTwo]]
表达式
foo[bar]
是对
foo
名为
bar
的属性的引用。属性键总是字符串或数字(从技术上讲只是字符串,但如果我们讨论的是数组,则将键视为数字更有意义)。因此,当JavaScript看到表达式
dice[[dieOne][dieTwo]]
时,它试图将
[dieOne][dieTwo]
强制为有效的属性键。怪事随之而来,如下所示:

  • [dieOne]
    计算为一个数组,该数组在索引0处包含一个元素,其值为
    dieOne
    ;让中间结果在下文中称为
    foo
  • foo[dieTwo]
    计算为对
    foo
    属性的引用,其索引为
    dieTwo
  • 因为,在循环的每次迭代中,
    dieTwo
    总是>0,而
    foo
    是一个数组,其唯一有效索引是
    0
    dieTwo
    是不受限制的。遗憾的是,数组返回
    未定义
    ,而不是抛出错误
  • undefined
    被强制为字符串,因此可以用作属性键。从技术上讲,只有字符串是属性键;根据标准,数组是伪造的。该值为
    “未定义”
  • 由于您的代码没有为
    dice[“undefined”]
    ,因此第一次尝试
    ++
    时,它会再次将其初始值视为
    undefined
    。同样,它没有抛出异常,而是不幸地将
    undefined
    强制为您想要的数值
    0
    ,递增为
    1
    ,并将其分配给新定义的
    骰子[“undefined”]
    • 如果您的浏览器遵循ES5标准,
      undefined++
      将是
      NaN
  • 由于遵循上述步骤,
    [dieOne][dieTwo]
    总是被强制为
    “未定义”
    ,因此
    骰子的上述属性在循环的每次迭代中都会增加一次,最终值为
    30000
    • 如果您的浏览器遵循ES5标准,
      NaN++
      将是
      NaN
      ,无论您将其增加多少次
  • 由于任何
    [foo][bar]
    其中
    不是
    0
    ,当强制为属性键时,将根据上述步骤
    “未定义”
    骰子[[n][m]
    始终等同于
    骰子[“未定义”]
    。只是为了好玩,试着用
    dice[[n][0]]
    ,其中
    n
    绝对不是
    “未定义”
    ,以验证它不是
    30000
    ,因此我是对的,应该打一个复选标记=D
这就是为什么你会得到这个特殊的结果

JS中没有真正的多维数组,如果您将其视为数组数组,那么您对语法的困惑就会小得多。然后您可以将其分解为以下步骤:

  • dice
    是一个数字数组
  • 因此,
    dice[n]
    是一个数字数组
  • 所以,
    dice[n][m]
    是我想要的数字
下面是您的正确程序的大致情况:

/* We don't like undefined anymore, so make an array of 7 arrays of 7 zeroes */
/* (we need 7 because JS array indexes start at 0 and you're using values 1-6) */
var dice = [
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0]
];
/* Now I'm tired of repeating myself, so let's DRY things up */
function roll() {
    return Math.floor(Math.random()*6) + 1;
}
var dieOne, dieTwo;
for ( var i = 0 ; i < 30000 ; i++ ) {
    dieOne = roll();
    dieTwo = roll();
    dice[dieOne][dieTwo]++;
}
/*我们不再喜欢未定义的,所以创建一个由7个零组成的7个数组*/
/*(我们需要7,因为JS数组索引从0开始,而您使用的是值1-6)*/
变量骰子=[
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0]
];
/*现在我已经厌倦了重复我自己的话,所以让我们把事情干了*/
功能辊(){
返回Math.floor(Math.random()*6)+1;
}
var dieOne,dieTwo;
对于(变量i=0;i<30000;i++){
dieOne=滚动();
dieTwo=滚动();
骰子[二酮][二酮]+;
}
要看的最重要的部分是最后一行

  • dice
    是一个数字数组
  • 所以,
    dice[dieOne]
    是一个数字数组
  • 因此,
    dice[dieOne][dieTwo]
    是一个我们可以有意义地增加的数字

您希望它做什么?您希望如何成为您的输出数组?我希望数组在1到6之间的随机点上增加1。例如,如果dieOne=5和dieTwo=3,我希望骰子[5][3]增加1。实际上
NaN
<代码>骰子[[dieOne][dieTwo]]++
没有任何意义,因为
[dieOne][dieTwo]
无法转换为整数…很有趣。。我从来没有让南站在我这边。只是一个有30000个的弹出窗口。使用Safari。我尝试过在循环中将所有索引设置为零,比如so dice[[I][j]]=0;但它仍然不起作用。也许我误解了你所说的“未定义”的意思
/* We don't like undefined anymore, so make an array of 7 arrays of 7 zeroes */
/* (we need 7 because JS array indexes start at 0 and you're using values 1-6) */
var dice = [
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0]
];
/* Now I'm tired of repeating myself, so let's DRY things up */
function roll() {
    return Math.floor(Math.random()*6) + 1;
}
var dieOne, dieTwo;
for ( var i = 0 ; i < 30000 ; i++ ) {
    dieOne = roll();
    dieTwo = roll();
    dice[dieOne][dieTwo]++;
}