Initializing and filling multi dimension array in javascript

后端 未结 2 1008
有刺的猬
有刺的猬 2021-01-18 12:23

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,

2条回答
  •  滥情空心
    2021-01-18 12:47

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

提交回复
热议问题