0

I can't seem to figure this out. I just get an undefined return.

let test = [1, 2, 3, 4, [6, 7, 8]];

How do I return the index [2] of test[4]?

I'm not even sure I'm asking the question properly. Basically, I want to interact with 8.

2 Answers 2

1

To maybe help you understand what is going on. If you write

let test = [1, 2, 3, 4, [6, 7, 8]];

you create an array (which is more like a list if you compare it to other languages). Every entry has its own datatype. So in the example we have the first 4 elements which are just numbers and the fifth entry which is another Array.

With the [] operator we address certain elements inside the array. If we want the first entry we can use test[0] and should get back 1.

You now want to access an element in the array inside an array. So you first address the array in the array. test[4] this will give you back [6, 7, 8] and now you can do the same thing again and address this new array. You could write it this way

let test = [1, 2, 3, 4, [6, 7, 8]];
let innerArray = test[4];
let element = innerArray[2];

The example above is just to better understand what is going on. In practice you will just do test[4][2] and it will basically to the same as above.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

let test = [1, 2, 3, 4, [6, 7, 8]];
console.log(test[4][2])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.