var 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);
//2D array
let activities = [
['Work', 9],
['Eat', 1],
['Commute', 2],
['Play Game', 1],
['Sleep', 7]
];
const m = 4;
const n = 5;
let arr = new Array(m); // create an empty array of length n
for (var i = 0; i < m; i++) {
arr[i] = new Array(n); // make each element an array
}
console.log(arr); // Output: [ [ <5 empty items> ], [ <5 empty items> ], [ <5 empty items> ], [ <5 empty items> ] ]
var array = [
["0, 0", "1, 0", "2, 0", "3, 0", "4, 0"],
["0, 1", "1, 1", "2, 1", "3, 1", "4, 1"],
["0, 2", "1, 2", "2, 2", "3, 2", "4, 2"],
["0, 3", "1, 3", "2, 3", "3, 3", "4, 3"],
["0, 4", "1, 4", "2, 4", "3, 4", "4, 4"],
]; // Think of it as coordinates, array[x, y] x = 0; y = 0; is "0, 0" on
// this grid
console.log(array[3][3]); // returns "3, 3"
// If you use graphics (ex: p5js or JS Canvas) then this will be a 5x5 map.
// Useful for roguelikes and/or raycasters.
// multidimensional array
const data = [[1, 2, 3], [1, 3, 4], [4, 5, 6]];
let activities = [
['Work', 9],
['Eat', 1],
['Commute', 2],
['Play Game', 1],
['Sleep', 7]
];
Code language: JavaScript (javascript)