How can I create a two dimensional array in JavaScript?

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

    There is another solution, that does not force you to pre-define the size of the 2d array, and that is very concise.

    var table = {}
    table[[1,2]] = 3 // Notice the double [[ and ]]
    console.log(table[[1,2]]) // -> 3

    This works because, [1,2] is transformed into a string, that is used as a string key for the table object.

提交回复
热议问题