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
  • 14

2 Answers2

1

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

console.log(matrix(4));
KevBot
  • 16,116
  • 5
  • 48
  • 66
1

I like the following solution

Array.apply(0, {length: 5}).map(function() {return Array(5).fill(0)})
NS0
  • 5,736
  • 1
  • 11
  • 13