How can I create a two dimensional array in JavaScript?

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

    My approach is very similar to @Bineesh answer but with a more general approach.

    You can declare the double array as follows:

    var myDoubleArray = [[]];
    

    And the storing and accessing the contents in the following manner:

    var testArray1 = [9,8]
    var testArray2 = [3,5,7,9,10]
    var testArray3 = {"test":123}
    var index = 0;
    
    myDoubleArray[index++] = testArray1;
    myDoubleArray[index++] = testArray2;
    myDoubleArray[index++] = testArray3;
    
    console.log(myDoubleArray[0],myDoubleArray[1][3], myDoubleArray[2]['test'],) 
    

    This will print the expected output

    [ 9, 8 ] 9 123
    

提交回复
热议问题