Need help with Accessing MultiDimensional Arrays With Indexes

Need help with Accessing MultiDimensional Arrays With Indexes
0

#1

Tell us what’s happening:

Why should i use var myData = myArray[2][1]; to get myData = 8. I did not get this, Could someone help me out regarding this?

Your code so far

// Setup
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

// Only change code below this line.
var myData = myArray[2][1];

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

In the array above, there are 4 elements in it. Those elements are below with corresponding indexes:

0: [1,2,3]
1: [4,5,6]
2: [7,8,9]
3: [[10,11,12],13,14]

The value 8 is in the 3rd array/element (index of 2), so that is where the 2 comes from in myArray[2]

In the array of [7,8,9], there are 3 elements. Those elements are below with corresponding indexes:

0: 7
1: 8
2: 9

The value 8 is the 2nd element (index of 1), so that is where the 1 comes from in myArray[2][1]

To take this example one step further, if we wanted to reference the value 12 in myArray, we would use:

var myData = myArray[3][0][2];

The 3 represents the third element of the myArray which is an array itself of three elements. The 0 represents the first element of this sub array which is an array itself of three elements. The 2 represents the 3rd element in the previous sub array.


#3

Thank you so much. I understood now. :slight_smile: