JavaScript中的多维数组递增
我目前正在做一些非常基本的工作,但我有一个困难的时候。我希望在二维数组中获得一个随机索引,并在for循环每次翻转时将其递增一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
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]
强制为有效的属性键。怪事随之而来,如下所示:
计算为一个数组,该数组在索引0处包含一个元素,其值为[dieOne]
;让中间结果在下文中称为dieOne
foo
计算为对foo[dieTwo]
属性的引用,其索引为foo
dieTwo
- 因为,在循环的每次迭代中,
总是>0,而dieTwo
是一个数组,其唯一有效索引是foo
,0
是不受限制的。遗憾的是,数组返回dieTwo
,而不是抛出错误未定义
- 值
被强制为字符串,因此可以用作属性键。从技术上讲,只有字符串是属性键;根据标准,数组是伪造的。该值为undefined
“未定义”
- 由于您的代码没有为
,因此第一次尝试dice[“undefined”]
时,它会再次将其初始值视为++
。同样,它没有抛出异常,而是不幸地将undefined
强制为您想要的数值undefined
,递增为0
,并将其分配给新定义的1
。骰子[“undefined”]
- 如果您的浏览器遵循ES5标准,
将是undefined++
NaN
- 如果您的浏览器遵循ES5标准,
- 由于遵循上述步骤,
总是被强制为[dieOne][dieTwo]
,因此“未定义”
骰子的上述属性在循环的每次迭代中都会增加一次,最终值为
。30000
- 如果您的浏览器遵循ES5标准,
将是NaN++
,无论您将其增加多少次NaN
- 如果您的浏览器遵循ES5标准,
- 由于任何
其中[foo][bar]
不是条
,当强制为属性键时,将根据上述步骤0
,“未定义”
始终等同于骰子[[n][m]
。只是为了好玩,试着用骰子[“未定义”]
,其中dice[[n][0]]
绝对不是n
,以验证它不是“未定义”
,因此我是对的,应该打一个复选标记=D30000
是一个数字数组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]
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]++;
}