1// declaration of a two-dimensional array
2// 5 is the number of rows and 4 is the number of columns.
3const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));
4
5console.log(matrix[0][0]); // 0
6
1
2
3
4
5 activities.forEach((activity) => {
6 activity.forEach((data) => {
7 console.log(data);
8 });
9});
1var iMax = 20;
2var jMax = 10;
3var f = new Array();
4
5for (i=0;i<iMax;i++) {
6 f[i]=new Array();
7 for (j=0;j<jMax;j++) {
8 f[i][j]=0;
9 }
10}
11
1var array = [
2 ["0, 0", "1, 0", "2, 0", "3, 0", "4, 0"],
3 ["0, 1", "1, 1", "2, 1", "3, 1", "4, 1"],
4 ["0, 2", "1, 2", "2, 2", "3, 2", "4, 2"],
5 ["0, 3", "1, 3", "2, 3", "3, 3", "4, 3"],
6 ["0, 4", "1, 4", "2, 4", "3, 4", "4, 4"],
7 ]; // Think of it as coordinates, array[x, y] x = 0; y = 0; is "0, 0" on
8 // this grid
9
10console.log(array[3][3]); // returns "3, 3"
11// If you use graphics (ex: p5js or JS Canvas) then this will be a 5x5 map.
12// Useful for roguelikes and/or raycasters.
1var grid = [];
2iMax = 3;
3jMax = 2;
4count = 0;
5
6 for (let i = 0; i < iMax; i++) {
7 grid[i] = [];
8
9 for (let j = 0; j < jMax; j++) {
10 grid[i][j] = count;
11 count++;
12 }
13 }
14
15// grid = [
16// [ 0, 1 ]
17// [ 2, 3 ]
18// [ 4, 5 ]
19// ];
20
21console.log(grid[0][2]); // 4
1var items = [
2 [1, 2],
3 [3, 4],
4 [5, 6]
5];
6console.log(items[0][0]); // 1
7console.log(items[0][1]); // 2
8console.log(items[1][0]); // 3
9console.log(items[1][1]); // 4
10console.log(items);