Javascript nestedArray

please can someone help explain this complex nested array and how to target second to the last one​:pray::pray:

let nestedArray = [
    ['deeper'], ['deeper'] 
      ['deepest'], ['deepest']

What do you need explanation for? It’s an array with arrays inside. And then these have arrays inside them.
You adress them like you adress normal arrays, just stacked.

nestedArray[0] == ["deep"]
nestedArray[1][0][0] == "deeper"
nestedArray[2][1] == [["deepest-est?"]]

Indentation helps telling the different layers apart.

thanks, but that number inserted in bracket notations are they the index number of the array

Yupp, and he showed you how you can get to them.

The first array has just one element.
The second has 2 arrays nested in it. At the same level.
The third has 2 arrays and they are needed even deeper.

sorry @Jagaya if I’m wrong might be tired but I think it’s like that:

nestedArray[0] == ["deep"]
nestedArray[1][0] == "deeper" and nestedArray[1][1] == "deeper"
nestedArray[2][1][0][0] == "deepest-est?"

Yeah just noticed that you left the 2 brackets at the last one. My bad:D

1 Like

thanks so much, u rock :boom:

this is a way to see it rapresented

Thanks to javaScriptTutor

1 Like

No for the index1 → that’s an array with two single-element arrays inside :wink:

nestedArray[1] == [['deeper'], ['deeper']]
nestedArray[1][0] == ['deeper']
nestedArray[1][0][1] == 'deeper'

Hah, that’s strange. Just tested it out and both your version and mine give the results when I try.
Still doesn’t seem like the best way to structure an array :sweat_smile:

Good thing someone recently had some issues with arrays → turns out JS is able to treat one-element arrays as if they were just the element, without an array.
So it might have just done that in your solution. If mine was incorrect, it would have gone one layer deeper with the index. And as strings are secretly immutable arrays (kinda) it should have shown just the characte at index 1 “e”.

If you add a second element to the array, there should be a difference inbetween our solutions :wink:

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