6

How do we initialize and create new multi dimension array?

Let's imagine if I want to initialize a 4x4 multi dimension array and fill it with 0's

Ideally, in 2D arrays, we would do

let oned_array = new Array(10).fill(0); // would create array of size 10 and fill it up with 0

How would I do something like [[0,0], [0,0]] (2x2 matrix)

let matrix = new Array([]).fill(0); 

I'm trying to solve few algorithm problems and this requires me creating a new multi dimension array and going through them (Island problem etc)

Please advise.

EDIT:

Another solution I found:

Array(2).fill(0).map(_ => Array(2).fill(0));

TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • 1
    did you aware that recommended identifier name format `[a-zA-z][a-zA-Z0-9_]+` – Sagar V Aug 22 '17 at 18:56
  • @Redo thank you for your response. Can you elucidate further? I'm trying to understand what you meant by 'identifier name format' can you state an example? – TechnoCorner Aug 22 '17 at 19:21
  • He means you can't begin a variable name with a number. – rphv Aug 22 '17 at 19:22
  • avoid using variables such as `1Darray` that is starting with a non alpha character. most of the languages will give you error – Sagar V Aug 22 '17 at 19:22
  • Possible duplicate of [How can I create a two dimensional array in JavaScript?](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – rphv Aug 22 '17 at 19:23
  • corrected @rphv :-) – TechnoCorner Aug 22 '17 at 19:30

2 Answers2

3

To get an independent filled array, you could use Array.from and map a new array with mapped values.

var array = Array.from({ length: 4 }, _ => Array.from({ length: 4 }, _ => 4));

array[0][0] = 0;
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Can we do something like ` a = Array(5).fill(0).map(x => Array(10).fill(0))` But my question was instead of looping through an array and creating another, can't we have a shorthand like .fill to create/fill mutli dimension arrays? – TechnoCorner Aug 22 '17 at 19:22
  • actually `fill` uses a value for every element, if that is an array, you insert the same object reference. – Nina Scholz Aug 22 '17 at 19:26
2

@NinaScholz' answer is much better—as always—but I finished writing this before seeing that answer so have decided to post anyway.

Similar idea but without the use of Array.from. It creates a new array at the specified length, fills it with 0's so that it can be iterated, iterates it to replace 0's with a new array of the specified length filled with the specified value.

    
const buildMatrix = (l, h, v) => Array(l).fill(0).map(_ => Array(h).fill(v));
const matrix = buildMatrix(4,4,4);
matrix[0][0] = 0;
console.log(matrix);
.as-console-wrapper { max-height: 100% !important; }
  • Thanks for your post. Can i re-write this to `var test2 = new Array(2).fill(new Array(2).fill(0))` – TechnoCorner Aug 22 '17 at 19:51
  • It would be `Array(2).fill(0).map(_ => Array(2).fill(0));` The map is important, otherwise it would fill the array with references to the same array, instead of creating a new array for each element. –  Aug 22 '17 at 20:36