Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Multi Dimensional Arrays in Javascript
Multi-dimensional arrays in JavaScript are arrays that contain other arrays as elements. They're useful when you need to organize data in rows and columns, like storing temperatures for each day of the week at different time intervals.
Creating Multi-Dimensional Arrays
Instead of creating separate arrays for each day:
let monday = [35, 28, 29, 31];
let tuesday = [33, 24, 25, 29];
console.log("Monday temperatures:", monday);
console.log("Tuesday temperatures:", tuesday);
Monday temperatures: [ 35, 28, 29, 31 ] Tuesday temperatures: [ 33, 24, 25, 29 ]
You can use a multi-dimensional array to store all temperature data in one structure:
let temps = [
[35, 28, 29, 31], // Monday
[33, 24, 25, 29], // Tuesday
[30, 26, 27, 28] // Wednesday
];
console.log("Temperature matrix:", temps);
Temperature matrix: [ [ 35, 28, 29, 31 ], [ 33, 24, 25, 29 ], [ 30, 26, 27, 28 ] ]
Accessing Elements
Use bracket notation with two indices: array[row][column]. The first index represents the row (outer array), and the second represents the column (inner array element):
let temps = [
[35, 28, 29, 31],
[33, 24, 25, 29]
];
console.log("First row, first element:", temps[0][0]); // Row 0, Column 0
console.log("Second row, third element:", temps[1][2]); // Row 1, Column 2
console.log("Entire first row:", temps[0]);
First row, first element: 35 Second row, third element: 25 Entire first row: [ 35, 28, 29, 31 ]
Iterating Through Multi-Dimensional Arrays
Use nested loops to traverse all elements:
let temps = [
[35, 28, 29, 31],
[33, 24, 25, 29]
];
for (let i = 0; i < temps.length; i++) {
console.log("Day #" + i);
for (let j = 0; j < temps[i].length; j++) {
console.log(" Time " + j + ": " + temps[i][j] + "°C");
}
}
Day #0 Time 0: 35°C Time 1: 28°C Time 2: 29°C Time 3: 31°C Day #1 Time 0: 33°C Time 1: 24°C Time 2: 25°C Time 3: 29°C
Three-Dimensional Arrays
Multi-dimensional arrays can have more than two dimensions. Here's a 3D array representing temperature data for multiple weeks:
let weeklyTemps = [
[ // Week 1
[35, 28, 29, 31], // Monday
[33, 24, 25, 29] // Tuesday
],
[ // Week 2
[32, 26, 27, 30], // Monday
[31, 23, 24, 28] // Tuesday
]
];
console.log("Week 1, Monday, 1st reading:", weeklyTemps[0][0][0]);
console.log("Week 2, Tuesday, 3rd reading:", weeklyTemps[1][1][2]);
Week 1, Monday, 1st reading: 35 Week 2, Tuesday, 3rd reading: 24
Common Use Cases
Multi-dimensional arrays are commonly used for:
- Matrices in mathematical calculations
- Game boards (chess, tic-tac-toe)
- Image pixel data
- Tabular data representation
- 3D graphics and physics calculations
Conclusion
Multi-dimensional arrays provide an organized way to store related data in rows and columns. Use nested loops to iterate through them, and remember that access follows the pattern array[row][column] for 2D arrays.
