Answers for "create 2d array in javascript filled with 0"

0

creating 2d array in javascript

var [r, c] = [5, 5]; 
var m = Array(r).fill().map(()=>Array(c).fill(0));
Posted by: Guest on October-16-2020
0

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]]
Posted by: Guest on September-26-2020

Code answers related to "create 2d array in javascript filled with 0"

Code answers related to "Javascript"

Browse Popular Code Answers by Language