The following example defines a two-dimensional array named activities:
let activities = [
['Work', 9],
['Eat', 1],
['Commute', 2],
['Play Game', 1],
['Sleep', 7]
];
In the activities array, the first dimension represents the activity and the second one shows the number of hours spent per day for each.
To show the activities array in the console, you use the console.table() method as follows:
console.table(activities);
The following illustrates the output:
┌─────────┬─────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼─────────────┼───┤
│ 0 │ 'Work' │ 9 │
│ 1 │ 'Eat' │ 1 │
│ 2 │ 'Commute' │ 2 │
│ 3 │ 'Play Game' │ 1 │
│ 4 │ 'Sleep' │ 7 │
└─────────┴─────────────┴───┘
Note that the (index)
column is for the illustration that indicates the indices of the inner array.
To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.
The following example returns the second element of the first inner array in the activities array above:
console.log(activities[0][1]); // 9
Adding elements to the JavaScript multidimensional array
You can use the Array methods such as push()
and splice()
to manipulate elements of a multidimensional array.
For example, to add a new element at the end of the multidimensional array, you use the push()
method as follows:
activities.push(['Study',2]);
┌─────────┬─────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼─────────────┼───┤
│ 0 │ 'Work' │ 9 │
│ 1 │ 'Eat' │ 1 │
│ 2 │ 'Commute' │ 2 │
│ 3 │ 'Play Game' │ 1 │
│ 4 │ 'Sleep' │ 7 │
│ 5 │ 'Study' │ 2 │
└─────────┴─────────────┴───┘
To insert an element in the middle of the array, you use the splice()
method. The following inserts an element in the second position of the activities array:
activities.splice(1, 0, ['Programming', 2]);
┌─────────┬───────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼───────────────┼───┤
│ 0 │ 'Work' │ 9 │
│ 1 │ 'Programming' │ 2 │
│ 2 │ 'Eat' │ 1 │
│ 3 │ 'Commute' │ 2 │
│ 4 │ 'Play Game' │ 1 │
│ 5 │ 'Sleep' │ 7 │
│ 6 │ 'Study' │ 2 │
└─────────┴───────────────┴───┘
This example calculates the percentage of the hours spent on each activity and appends the percentage to the inner array.
activities.forEach(activity => {
let percentage = ((activity[1] / 24) * 100).toFixed();
activity[2] = percentage + '%';
});
┌─────────┬───────────────┬───┬───────┐
│ (index) │ 0 │ 1 │ 2 │
├─────────┼───────────────┼───┼───────┤
│ 0 │ 'Work' │ 9 │ '38%' │
│ 1 │ 'Programming' │ 2 │ '8%' │
│ 2 │ 'Eat' │ 1 │ '4%' │
│ 3 │ 'Commute' │ 2 │ '8%' │
│ 4 │ 'Play Game' │ 1 │ '4%' │
│ 5 │ 'Sleep' │ 7 │ '29%' │
│ 6 │ 'Study' │ 2 │ '8%' │
└─────────┴───────────────┴───┴───────┘
Removing elements from the JavaScript multidimensional array
To remove an element from an array, you use the pop()
or splice()
method.
For example, the following statement removes the last element of the activities array:
activities.pop();
┌─────────┬───────────────┬───┬───────┐
│ (index) │ 0 │ 1 │ 2 │
├─────────┼───────────────┼───┼───────┤
│ 0 │ 'Work' │ 9 │ '38%' │
│ 1 │ 'Programming' │ 2 │ '8%' │
│ 2 │ 'Eat' │ 1 │ '4%' │
│ 3 │ 'Commute' │ 2 │ '8%' │
│ 4 │ 'Play Game' │ 1 │ '4%' │
│ 5 │ 'Sleep' │ 7 │ '29%' │
└─────────┴───────────────┴───┴───────┘
Similarly, you can remove the elements from the inner array of the multidimensional array by using the pop()
method. The following example removes the percentage element from the inner arrays of the activities array.
activities.forEach((activity) => {
activity.pop(2);
});
┌─────────┬───────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼───────────────┼───┤
│ 0 │ 'Work' │ 9 │
│ 1 │ 'Programming' │ 2 │
│ 2 │ 'Eat' │ 1 │
│ 3 │ 'Commute' │ 2 │
│ 4 │ 'Play Game' │ 1 │
│ 5 │ 'Sleep' │ 7 │
└─────────┴───────────────┴───┘
Iterating over elements of the JavaScript multidimensional array
To iterate a multidimensional array, you use a nested for
loop as in the following example.
// loop the outer array
for (let i = 0; i < activities.length; i++) {
// get the size of the inner array
var innerArrayLength = activities[i].length;
// loop the inner array
for (let j = 0; j < innerArrayLength; j++) {
console.log('[' + i + ',' + j + '] = ' + activities[i][j]);
}
}
The first loop iterates over the elements of the outer array and the nested loop iterates over elements of the inner array.
The following shows the output of the script in the console:
[0,0] = Work
[0,1] = 9
[1,0] = Eat
[1,1] = 1
[2,0] = Commute
[2,1] = 2
[3,0] = Play Game
[3,1] = 1
[4,0] = Sleep
[4,1] = 7
[5,0] = Study
[5,1] = 2
Or you can use the forEach()
method twice:
activities.forEach((activity) => {
activity.forEach((data) => {
console.log(data);
});
});
Work
9
Eat
1
Commute
2
Play Game
1
Sleep
7
Study
2
var arr2D = new Array(5).fill(new Array(3));
besides if you don't want the cells to be "undefined" you can do likevar arr2D = new Array(5).fill(new Array(3).fill("hey"));
– Quatrainvar arr2D = new Array(5).fill(new Array(3));
, each element of Array(5) will point to the same Array(3). So it's best to use a for loop to dynamically populate sub arrays. – Isadoraa = Array(5).fill(0).map(x => Array(10).fill(0))
– Recusancyvar arr2D = new Array(5).fill(new Array(3));
doesn't properly create a 2d array? – Dayenew Array(3)
call, since that is executed to create a new array, then that array is passed into the function to populate the one that you are filling... – Isadorafill
doesn't callnew Array(3)
for each index of the array being filled, since it's not a lambda expression or anything, such as Longfei Wu's comment above, which initially fills the array with 0's, then uses the map function with a lambda to fill each element with a new array. The fill function simply fills the array with exactly what you tell it to. Does that make sense? For more info on themap
function, see: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – IsadoraIf the new question is a better question or has better answers, then vote to close the old one as a duplicate of the new one.
– Hyponitritelet AA = Array.from({ length: 2 }, () => new Array(3).fill(0));
– Hiltonhilum