How can I create a two dimensional array in JavaScript?

后端 未结 30 4278
天涯浪人
天涯浪人 2020-11-21 05:25

I have been reading online and some places say it isn\'t possible, some say it is and then give an example and others refute the example, etc.

  1. How do I dec

30条回答
  •  孤街浪徒
    2020-11-21 05:51

    Javascript does not support two dimensional arrays, instead we store an array inside another array and fetch the data from that array depending on what position of that array you want to access. Remember array numeration starts at ZERO.

    Code Example:

    /* Two dimensional array that's 5 x 5 
    
           C0 C1 C2 C3 C4 
        R0[1][1][1][1][1] 
        R1[1][1][1][1][1] 
        R2[1][1][1][1][1] 
        R3[1][1][1][1][1] 
        R4[1][1][1][1][1] 
    */
    
    var row0 = [1,1,1,1,1],
        row1 = [1,1,1,1,1],
        row2 = [1,1,1,1,1],
        row3 = [1,1,1,1,1],
        row4 = [1,1,1,1,1];
    
    var table = [row0,row1,row2,row3,row4];
    console.log(table[0][0]); // Get the first item in the array
    

提交回复
热议问题