How can I create a two dimensional array in JavaScript?

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

    To create an 4x6 array, simply do this

    const x = [...new Array(6)].map(elem => new Array(4))
    

    It's usually a good practice to start with an empty array, rather than filling w random values. (You normally declare array as const x = [] in 1D, so better to start w empty in 2D.)

提交回复
热议问题