How to create a two dimensional array in JavaScript?


Posted by Rich on 2022-02-14

Simple way:

const array = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
 ]

another:

let x = new Array(10);

for (var i = 0; i < x.length; i++) {
  x[i] = new Array(3);
}

console.log(x);

I used this for my project:

 let arr = [];
    for (let i = 0; i < size; i++) {
      arr.push(new Array(size).fill(0));
    }
    console.log(arr)

from here










Related Posts

【JS幼幼班】Step.06 基本語法:基本型別(string、symbol)

【JS幼幼班】Step.06 基本語法:基本型別(string、symbol)

 建立屬於你的 Google Map 地圖標記(四) - 建立可拖曳的地圖標記

建立屬於你的 Google Map 地圖標記(四) - 建立可拖曳的地圖標記

Google Chrome開發者工具教學 - Elements

Google Chrome開發者工具教學 - Elements


Comments