5.6 2D Arrays
However, you can create a multidimensional array by defining an array of elements, where each element is also another array.
To declare an empty multidimensional array, you use the same syntax as declaring one-dimensional array:
let activities = ;
The following example defines a two-dimensional array named
let activities = [ ['Work', 9], ['Eat', 1], ['Commute', 2], ['Play Game', 1], ['Sleep', 7] ];
Here's what the code is doing conceptually:
activitiesarray, 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); // output: ┌─────────┬─────────────┬───┐ │ (index) │ 0 │ 1 │ ├─────────┼─────────────┼───┤ │ 0 │ 'Work' │ 9 │ │ 1 │ 'Eat' │ 1 │ │ 2 │ 'Commute' │ 2 │ │ 3 │ 'Play Game' │ 1 │ │ 4 │ 'Sleep' │ 7 │ └─────────┴─────────────┴───┘
You can add practice problems here if you want. Follow the format below.
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact firstname.lastname@example.org for inquiries.