How can I create a two dimensional array in JavaScript?

后端 未结 30 4342
天涯浪人
天涯浪人 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:40

    To create a 2D array in javaScript we can create an Array first and then add Arrays as it's elements. This method will return a 2D array with the given number of rows and columns.

    function Create2DArray(rows,columns) {
       var x = new Array(rows);
       for (var i = 0; i < rows; i++) {
           x[i] = new Array(columns);
       }
       return x;
    }
    

    to create an Array use this method as below.

    var array = Create2DArray(10,20);
    

提交回复
热议问题