Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

creating a 2d array in js

var x = new Array(10);

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

console.log(x);
Comment

creating 2d array in javascript

var [r, c] = [5, 5]; 
var m = Array(r).fill().map(()=>Array(c).fill(0));
Comment

2d array in js

a = Array(5).fill(0).map(x => Array(10).fill(0))
Comment

2d array javascript

var items =[
     
  [1,        2,       3],//this is row 0
  [4,        5,       6],//this is row 1
  [7,        8,       9] //this is row 2
//cullom 0   cullom 1   cullom2
  
]

console.log(/* variable name */ items[/*row*/ 0][/*cullom*/ 0]);
Comment

js initialize 2d array

let data = [];
for (let row=0; row<rows; row++) {
    data.push(new Array(cols).fill('#'));
};
Comment

2d array in js

var x = new Array(10);

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

console.log(x);
 Run code snippetHide results
Comment

create 2d array in javascript filled with 0

function zeros(dimensions) {
    var array = [];

    for (var i = 0; i < dimensions[0]; ++i) {
        array.push(dimensions.length == 1 ? 0 : zeros(dimensions.slice(1)));
    }

    return array;
}

> zeros([5, 3]);
  [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
Comment

2d array in javascript

function createArray(row,column) {
let arr = [];

for(var i=0; i<row; i++){
    arr[i] = [Math.floor(Math.random() * (10))];

    for(var j=0;j<column;j++){
        arr[i][j]= [Math.floor(Math.random() * (20))];
    }
}

return arr;
}

var arrVal = createArray(4, 5);

console.log(arrVal);
Comment

javascript creating and accessing 2d array

// creating 1d array
var array = new Array(10);

// add an array to each 1d element, hence creating 2d array
for (var i = 0; i < array.length; i++) {
    array[i] = new Array(2);
}

// accessing: first element of the first ro
console.log(array[0][0]);
Comment

2d array js

 2d array
Comment

2d array JS

<script>
  
// Create one dimensional array
    var gfg = new Array(2);
  
document.write("Creating 2D array <br>");
  
// Loop to create 2D array using 1D array
for (var i = 0; i < gfg.length; i++) {
    gfg[i] = new Array(2);
}
  
var h = 0;
  
// Loop to initialize 2D array elements.
for (var i = 0; i < 2; i++) {
    for (var j = 0; j < 2; j++) {
        gfg[i][j] = h++;
    }
}
  
// Loop to display the elements of 2D array. 
for (var i = 0; i < 2; i++) {
    for (var j = 0; j < 2; j++)    {
        document.write(gfg[i][j] + " ");
    }
    document.write("<br>");
} 
</script>
Comment

2d array JS

<script>
  
// Create one dimensional array
var gfg = new Array(3);
  
// Loop to create 2D array using 1D array
document.write("Creating 2D array <br>");
for (var i = 0; i < gfg.length; i++) {
    gfg[i] = [];
}
var h = 0;
var s = "GeeksforGeeks";
  
// Loop to initialize 2D array elements.
for (var i = 0; i < 3; i++) {
    for (var j = 0; j < 3; j++) {
  
        gfg[i][j] = s[h++];
    }
}
  
// Loop to display the elements of 2D array.
for (var i = 0; i < 3; i++) {
    for (var j = 0; j < 3; j++)
  
    {
        document.write(gfg[i][j] + " ");
    }
    document.write("<br>");
}
  
</script>
Comment

2d arrays js

let items = [
  [1, 2],
  [3, 4],
  [5, 6]
];
console.log(items[0][0]); // 1
console.log(items[0][1]); // 2
console.log(items[1][0]); // 3
console.log(items[1][1]); // 4
console.log(items);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: filter object js 
Javascript :: convert timestamp to time javascript 
Javascript :: get the last array element javascript 
Javascript :: sort method in js 
Javascript :: add class with jquery 
Javascript :: js delete json element 
Javascript :: process.argv 
Javascript :: react keys 
Javascript :: js copy image to clipboard 
Javascript :: combine 2 arrays javascript 
Javascript :: how to update node js through terminal 
Javascript :: sort array of objects javascript by key value 
Javascript :: inverse of stringify js 
Javascript :: remove comma from end of string javascript 
Javascript :: insert element after element javascript 
Javascript :: set localstorage value 
Javascript :: script src= https//kit.fontawesome.com/a81368914c.js /script 
Javascript :: textbox in javascript 
Javascript :: unshift method in javascript 
Javascript :: sequelize association helper methods 
Javascript :: js months ago 
Javascript :: javascript promises 
Javascript :: ordering array 
Javascript :: setinterval javascript 
Javascript :: js get index of item in array 
Javascript :: javascript fore each break example 
Javascript :: how to print a pdf 
Javascript :: sending value in input angular material 
Javascript :: js remove key from object 
Javascript :: react usecallback 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =