declare empty 2d array of size m * n in JS code example
Example 1: javscript generate empty 2d array
var grid = [...Array(width)].map(e => Array(height));
// access via grid[y][x]
Example 2: create 2d array in javascript filled with 0
function zeros(dimensions) {
var array = [];
for (var i = 0; i < dimensions[0]; ++i) {
array.push(dimensions.length == 1 ? 0 : zeros(dimensions.slice(1)));
}
return array;
}
> zeros([5, 3]);
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]