Skip to content Skip to sidebar Skip to footer

Initializing And Filling Multi Dimension Array In Javascript

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

Solution 1:

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; }

Post a Comment for "Initializing And Filling Multi Dimension Array In Javascript"