Complex multi-dimensional arrays help

Tell us what’s happening:

I added an array and don't understand why nestedArray[0][1] doesn't print deep1. I wanted to make sure I was understand the concept by logging to the console all the possible values. When I added deep1 it doesn't print although deep does print nestedArray[0][0].

Your code so far


let nestedArray = [ // top, or first level - the outer 
  ['deep'],['deep1'], // an array within an array, 2 
  [
    ['deeper'], ['deeper2'] // 2 arrays nested 3 levels deep
  ],
  [
    [
      ['deepest1'], ['deepest2'] // 2 arrays nested 4 levels
    ],
    [
      [
        ['deepest-est?'],["deepest-est1"] // 5 
      ]
    ]
  ]
];

console.log(nestedArray[0][1]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/create-complex-multi-dimensional-arrays

Try doing console.log(nestedArray[0]) first, and see what that gives you. It will surprise you.


All the other indexes I checked came out okay, so I am confused about the first 2 array positons i.e. nestedArray[0][0] and nestedArray[0][1].

console.log(nestedArray[2][1][0][1]);
**deepest-est1**

console.log(nestedArray[2][1][0][0]);
deepest-est?

console.log(nestedArray[2][0][0])
deepest1

console.log(nestedArray[2][0][1]);
deepest2

console.log(nestedArray[1][0]);
deeper

console.log(nestedArray[1][1]);
deeper2

console.log(nestedArray[0][0]);
deep```

Yes nestedArray[0] works , but I assumed you didn't need the [0][0] because it the name of the array.

And what did it show you?

I put your code, as-is, on repl. Take a look. that console.log statement? It returns

['deep']

and not

[ [ 'deep' ],[ 'deep1' ] ]

as it seems you want.

///Actually I just want deep1. The first level [0][0] I assumed we didn’t need since the bracket notation is really pointed arithmetic. Therefore I would need [0][1] the second element in the 1st array. Sorry, but that is where my thinking is. [0] works because the name of the array is the memory location of the first element.///

I want to say I always appreciate your help. I’m trying to gain clarity on the nested array thing.

Right. So you took the sample code on the left bar of that page, and you added ‘deep1’ to it. Which is fine, but you didn’t do what you think you did. Here’s what the structure would expand to, if we pretty it up:

let nestedArray = [ // top, or first level - the outer most array
  ['deep'], 
  ['deep1'], // an array within an array, 2 levels of depth
  [
    ['deeper'], ['deeper'] // 2 arrays nested 3 levels deep
  ],
  [
    [
      ['deepest'], ['deepest'] // 2 arrays nested 4 levels deep
    ],
    [
      [
        ['deepest-est?'] // an array nested 5 levels deep
      ]
    ]
  ]
];

Note that each indentation represents one layer deeper in the nested structure.

Instead, what you seem to want is something like this:

let nestedArray = [ // top, or first level - the outer most array
  [
    'deep', 'deep1' // an array **of strings** within an array, 2 levels of depth
  [
    ['deeper'], ['deeper'] // 2 arrays nested 3 levels deep
  ],
  [
    [
      ['deepest'], ['deepest'] // 2 arrays nested 4 levels deep
    ],
    [
      [
        ['deepest-est?'] // an array nested 5 levels deep
      ]
    ]
  ]
];

So the outer array contains three members: an array of two strings, an array of two arrays, and a DEEPLY nested ugly array.

console.log(nestedArray[0]) would now return 'deep', 'deep1', and nestedArray[0][1] would, in fact, be ‘deep1’.

Thanks, the library closed, so I’ll check it out later. You are always appreciated.