Declare an empty two-dimensional array in Javascript?

后端 未结 18 1403
暖寄归人
暖寄归人 2020-11-29 21:45

I want to create a two dimensional array in Javascript where I\'m going to store coordinates (x,y). I don\'t know yet how many pairs of coordinates I will have because they

18条回答
  •  天涯浪人
    2020-11-29 22:30

    If you want to initialize along with the creation, you can use fill and map.

    const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));
    

    5 is the number of rows and 4 is the number of columns.

提交回复
热议问题