JavaScript Multidimensional Arrays
In JavaScript, arrays are incredibly powerful structures that allow you to store multiple values. But did you know that JavaScript arrays can hold other arrays? These are known as multidimensional arrays. Multidimensional arrays are commonly used to represent more complex data structures, such as matrices or grids.
A multidimensional array is an array of arrays. This allows you to store data in a matrix-like structure, where each element can itself be an array. You can think of a multidimensional array as a table of rows and columns, with each row representing an array, and each column being an element within that array.
For example, a 2D array (two-dimensional array) can represent a grid with rows and columns.
You can create a multidimensional array by embedding arrays within an array:
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Here, matrix
is a 2D array with 3 rows and 3 columns.
To access elements in a multidimensional array, you need to use two indices: one for the row and one for the column. The first index specifies the row, and the second index specifies the column within that row.
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Access the element in the second row and third column
console.log(matrix[1][2]); // Output: 6
Explanation:
matrix[1]
accesses the second row: [4, 5, 6]
.matrix[1][2]
accesses the third element in the second row, which is 6
.You can modify the elements of a multidimensional array in the same way you access them—by specifying the row and column.
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Modify the element in the third row and second column
matrix[2][1] = 10;
console.log(matrix); // Output: [[1, 2, 3], [4, 5, 6], [7, 10, 9]]
Explanation:
matrix[2][1]
accesses the second column in the third row and modifies it to 10
.You can also create a multidimensional array dynamically, based on conditions or loops. This is useful when you don’t know the exact size of the array in advance.
let rows = 3;
let cols = 3;
let matrix = [];
// Create a 3x3 matrix using a loop
for (let i = 0; i < rows; i++) {
matrix[i] = []; // Initialize each row as an empty array
for (let j = 0; j < cols; j++) {
matrix[i][j] = i * cols + j + 1; // Assign values
}
}
console.log(matrix);
// Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Explanation:
rows
and cols
, creating an empty array for each row and then filling each element with a calculated value.Iterating over a multidimensional array requires nested loops: one for the rows and one for the columns. Alternatively, you can use methods like forEach()
or map()
to simplify iteration.
for
Loops
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]); // Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
}
}
Explanation:
matrix.length
gives the number of rows).matrix[i].length
gives the number of columns in the current row).forEach()
Method
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
matrix.forEach(function(row) {
row.forEach(function(element) {
console.log(element); // Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
});
});
Explanation:
forEach()
method is used to iterate over each row of the matrix.forEach()
iterates over each element within the row.While 2D arrays (matrices) are the most common, JavaScript also supports higher-dimensional arrays (3D, 4D, etc.). These arrays can be accessed in a similar way but involve additional layers of indexing.
let threeDArray = [
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
];
// Access an element in the second 2D array and first row
console.log(threeDArray[1][0][1]); // Output: 6
Explanation:
threeDArray[1]
accesses the second 2D array: [[5, 6], [7, 8]]
.threeDArray[1][0]
accesses the first row in that array: [5, 6]
.threeDArray[1][0][1]
accesses the second element in that row, which is 6
.Multidimensional arrays are commonly used in scenarios where data is structured in a grid or table format. Some use cases include: