簡體   English   中英

在JavaScript中增加多維數組

[英]Incrementing Multi Dimensional Array in JavaScript

我目前正在做一些非常基本的事情,但我很難過。 我希望在二維數組中得到一個隨機索引,並在每次for循環翻轉時將其遞增1。

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]]);

為什么這個for循環將所有索引設置為30,000? 我錯誤地使用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"

你可能想做什么

它看起來像你真正想要的Array 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..
}

然后說你想知道你有多少次dieOne = 1,dieTwo = 3 ,你會看

dice[1][3]; // count the brackets again.

這個(不幸的是)技術上有效的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而不是拋出錯誤。
  • undefined值被強制轉換為字符串,因此可以將其用作屬性鍵。 從技術上講,只有字符串才是屬性鍵; 根據標准,陣列有點偽造。 值為"undefined"
  • 由於你的代碼沒有為dice["undefined"]分配一個值,所以第一次嘗試++它會將其初始值視為undefined 而且,不幸的是,它不是拋出異常,而是將undefined強制轉換為您想要的內容,數字0 ,遞增為1 ,並將其分配給新定義的dice["undefined"]
    • 如果您的瀏覽器遵循ES5標准,則undefined++將是NaN
  • 由於遵循上述步驟, [dieOne][dieTwo]始終被強制為"undefined"dice屬性在循環的每次迭代中遞增一次,最終值為30000
    • 如果您的瀏覽器遵循ES5標准,那么NaN++將是NaN ,無論您增加多少次。
  • 由於任何[foo][bar]其中bar不為0 ,當強制轉換為屬性鍵時,根據上述步驟將"undefined"dice[[n][m]]總是相當於dice["undefined"] 只是為了好玩,嘗試dice[[n][0]] ,其中n絕對是"undefined"以外的任何東西,以驗證它不是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]++;
}

最重要的部分是最后一行。

  • dice是一組數字,
  • 所以dice[dieOne]是一個數字數組,
  • 所以dice[dieOne][dieTwo]是一個我們可以有意義地增加的數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM