Multi dimensional arrays

Tell us what’s happening:
Describe your issue in detail here.

I cant get my head around this…

I watched a you tube vid and it said to access element " blue" in this , for example
let colors = [
[‘yellow’, ‘pink’ [ 'blue]]
]

would be colors[0][2][0]

is this right?

I’ve also looked at the answer for this challange.

and when i do this,

let myNestedArray = [
// Only change code below this line
[‘unshift’, false, 1, 2, 3, ‘complex’, ‘nested’ ],
[‘loop’, ‘shift’, 6, 7, 1000, ‘method’],
[‘concat’, false, true, ‘spread’, ‘array’ [“deep”]], //<<<< i added this ‘deep’
[‘mutate’, 1327.98, ‘splice’, ‘slice’, ‘push’],
[‘iterate’, 1.3849, 7, ‘8.4876’, ‘arbitrary’, ‘depth’]
// Only change code above this line
];

console.log(myNestedArray[2][6][0])

TypeError: Cannot read property ‘0’ of undefined << why is this coming up when i have copied something from this answers?

Are all the lines in the example all level 2 as no nesting has been done yet?
And does each time you do a e.g my NestedArray[0][1][2] << this would refer to 3 depths of the array and the third depth would be an array, inside an array inside an array?

In the example it gives:

Ive tried to label the levels, is this correct.

to access the second deepest i thought i would do
console.log(nestedArray[3][2][0])

but i just get error: TypeError: Cannot read property ‘2’ of undefined

let nestedArray = [  // level 0
  ['deep'],  // level 1
  [  // level 1
    ['deeper'], ['deeper']  // level 2
  ],
  [ //  level 1
    [ // level 2
      ['deepest'], ['deepest'] level 3
    ],  level 2
    [ // level 3
      [ // level 4
        ['deepest-est?'] level 5
      ] // level 4
    ] // level 3
  ] // level 2
]; // level 1
console.log(nestedArray[2][1][0][0][0]); <<< = deepest-est?

but when i console log it, console.log(nestedArray[2][1]), also equals deepest-est? so why do you need to put the 5 level of depth when the result is found at just 2?? 

Sorry, i hope this makes sense, i really cant get past this bit :( 
  **Your code so far**

let myNestedArray = [
// Only change code below this line
['unshift', false, 1, 2, 3, 'complex', 'nested'],
['loop', 'shift', 6, 7, 1000, 'method'],
['concat', false, true, 'spread', 'array' ["deep"]],
['mutate', 1327.98, 'splice', 'slice', 'push'],
['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
// Only change code above this line
];

console.log(myNestedArray[2][6][0])

Hope someone can help me get my head around this.

thanks

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Create complex multi-dimensional arrays

Link to the challenge:

[ 'level 0' [ 'level 1' deep ] ]

If deep is level 1, then deeper is level 2, deepest level 3 and deepest-est is level 4. There is no level 5.

Think about an array as of a box.

let arr1 = []

An empty box, nothing to do here.
Let’s move on.

let arr2 = ['some', 'nice', 'variables', 12, 777] 

A box with something inside.

You can print whole box console.log(arr2)

OR you can print one thing from inside by writing arr2[1]
Try it.

Ok, let’s move on.

let arr3 = [[], [], [], "NOT a box!"]

Another empty box, but it has 3 empty boxes inside and a string.
You can access any box (or a string) same way as before. arr3[1], arr3[3] etc. Try it.

Ok, let’s move on.

let arr4 = [
["insidebox0", 12, 3232, 4],
["insidebox1", 22, 333],
["insidebox2", ["oh no, another box?"]],
"NOT a box!"]

Now, this is getting interesting.
Boxes can contain another boxes

Try:
console.log(arr4)
console.log(arr4[0])
console.log(arr4[3])
console.log(arr4[2])
console.log(arr4[2][0])

You should understand it by now.

Also, you forgot to add one coma “,” in your myNestedArray array. Just before ["deep"].

1 Like

I would suggest when trying to understand this that instead of just chaining the brackets without checking that you got each level correctly you do each of the levels on its own and log it out each time so you know you are on the right track.

No, it is:

console.log(nestedArray[2][1]) // [ [ [ 'deepest-est?' ] ] ]
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.