Javascript in 15 Days : Multidimensional Arrays

March 12th, 2020

Welcome back!

Before we begin, I’d like to make sure you’ve checked out yesterday’s story. If you have, well done! Today we’ll be talking about multidimensional arrays! If you haven’t, feel free to check out yesterday's topic on the link below!

Javascript in 15 Days : Arrays

Introduction

So, this is a continuation of yesterday's topic, arrays. As you've noticed, when we discussed about loops, we discussed about nested loops, or loop inside a loop, and now, the same concept will be applied to arrays. Array, as discussed, can hold any data type, which can also mean, it can hold arrays, which are called multidimensional arrays, or if it makes you easier, array in arrays. Now, let's talk about how a multidimensional array looks.

multidimensional array

Structure Explanation

Now, as you can see above, we declared a variable students with its structure being a multidimensional array, and when we run the script, the console.log on line 6 will print the correct output, which can be seen above (on line 8 ~ 12).

Demonstration

Now that you know the structure, let's move up the ladder. As you've noticed, since multidimensional arrays are by default, of type array, all methods and properties on an array can also be used in multidimensional arrays. Now, let's try manipulating the students variable. FYI, the inner array, by default consists of 3 values, with the correct order of name, age, gender.

Reassign existing student with new record

reassign(array)

Appending a new student recording

append

Prepending a new student recording

preprend

Removing student from the front

remove(front)

Removing student from the back

remove(back)


Now that we're done covering the basic methods of manipulating a whole array, let's see what happens if we want to manipulate the inner array, or the element itself.

Reassign existing student's name

reassign(element)

Append new value(hobby) in a student's record

append(hobby)

Prepend new value(hobby) in a student's record

prepend(hobby)

Remove existing student's name in a specified index (from the front)

remove(front)

Remove existing student's gender in a specified index (from the back)

remove(back)

Summary

So, as you can see, it's exactly like yesterday, but a little more advanced. Look, if you don't really get it, that's fine, you'll slowly get the hang of it with a lot of practice. I got a challlenge for you, try to use the splice and slice on the multidimensinal array, and hit me up if you're done or stuck on Twitter or LinkedIn. Have a great day!

Javascript in 15 Days : Objects