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

搜尋資料庫所有欄位

搜尋資料庫所有欄位

寫程式之餘,書籤列也要斷捨離整理

寫程式之餘,書籤列也要斷捨離整理

如何打包 CRA 專案並建立不透過第三方服務即可供別人使用的專案

如何打包 CRA 專案並建立不透過第三方服務即可供別人使用的專案


Comments