Declare an empty two-dimensional array in Javascript?

后端 未结 18 1394
暖寄归人
暖寄归人 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:47

    If we don’t use ES2015 and don’t have fill(), just use .apply()

    See https://stackoverflow.com/a/47041157/1851492

    let Array2D = (r, c, fill) => Array.apply(null, new Array(r)).map(function() {return Array.apply(null, new Array(c)).map(function() {return fill})})
    
    console.log(JSON.stringify(Array2D(3,4,0)));
    console.log(JSON.stringify(Array2D(4,5,1)));

提交回复
热议问题