0

I tried to make an array and then to map over it and declare new arrays but failed miserably.

let matrix = (new Array(5).fill(0)).map(new Array(5).fill(0));
Ioan Ungurean
  • 319
  • 1
  • 15

2 Answers2

1

function matrix(size) {
  return new Array(size).fill(0).map(el => Array(size).fill(0));
}

console.log(matrix(4));
KevBot
  • 17,900
  • 5
  • 50
  • 68
1

I like the following solution

Array.apply(0, {length: 5}).map(function() {return Array(5).fill(0)})
NS0
  • 6,016
  • 1
  • 15
  • 14