Access Multi-Dimensional Arrays With Indexes Explanation

Can someone explain the concept of accessing multi-dimensional arrays with indexes?

For example:

const arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [[10, 11, 12], 13, 14]


How is arr[3] = [[10, 11, 12], 13, 14]?
How is arr[3][0][1]= 11? Why is the [0] included in the index when it should just be [3][1]?

If I gave you the following array:

numberArray = [5, 6, 7, 8]

Then numberArray[3] would equal 8 because that is the value of the fourth item in the array.

But instead of numbers, the arr array contains arrays. But it’s still the same concept. So what array is in the fourth spot in arr?

1 Like

HI @Honeybee !

Dealing with nested arrays can be confusing at first.
It helps to remember that you can access elements from an array using bracket notation.

arr[3] tells the computer to access the element at index 3.
Each element in our array is separated by commas and we start counting at index 0.

Here is our array.

Here is arr[0]

Here is arr[1]

Here is arr[2]

Here is arr[3]

This is is special because there is an array nested inside.
We first have to tell the computer to go to element at index 3, which is this syntax :

Now this array has three elements.
First try to identify what those three elements following the same steps we did earlier when we were trying to find arr[3].

Then once you can identify those three elements, focus on the first one which is another array.

This array has three elements of its own.
Walk through those same steps as earlier, to identify those three elements.

By slowly walking through those steps it should help you understand how we got to this.

Well remember that arr[3] would be this.

and so arr[3][1] tells the computer to go to index 1 of arr[3].
The number 13 is the element at index 1 of arr[3]
Hope that helps! :smile:


Thanks a lot, it’s clear for me now !!