How can I create a two dimensional array in JavaScript?

后端 未结 30 4905
天涯浪人
天涯浪人 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条回答
  •  萌比男神i
    2020-11-21 05:40

    Javascript only has 1-dimensional arrays, but you can build arrays of arrays, as others pointed out.

    The following function can be used to construct a 2-d array of fixed dimensions:

    function Create2DArray(rows) {
      var arr = [];
    
      for (var i=0;i

    The number of columns is not really important, because it is not required to specify the size of an array before using it.

    Then you can just call:

    var arr = Create2DArray(100);
    
    arr[50][2] = 5;
    arr[70][5] = 7454;
    // ...
    

提交回复
热议问题