var x = new Array(10);
for (var i = 0; i < x.length; i++) {
x[i] = new Array(3);
}
console.log(x);
var [r, c] = [5, 5];
var m = Array(r).fill().map(()=>Array(c).fill(0));
a = Array(5).fill(0).map(x => Array(10).fill(0))
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]);
let data = [];
for (let row=0; row<rows; row++) {
data.push(new Array(cols).fill('#'));
};
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
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);
2d array
<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>
<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>
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