Bracket notation/indexes and nested array confusion

Bracket notation/indexes and nested array confusion
0

#1

How would the following work, I need some logical clarification please!

[[‘test’, ‘dog’],[2, ‘cat’], 2, 5, 7, 9]

How would indexing view this, would [8] = 9? As there are 8 total values or do I consider each individual nested array a entire value alone i.e. [6] = 9? I’m terribly confused and would appreciate any help!

Kind regards,
Ale8k


#2

First of all, index starts from 0 not 1. Back to your question.

If you tried to code this, the answer becomes immediately clear.

If an array contains an array element, let’s call it A. Then, A is treated as a single value instead of multiple values inside A.

var arr = [ [1, 2, 3] ] // Array of single array element
arr[0] // [1, 2, 3]
arr[0][0] // [1, 2, 3][0] = 1

The last one reads as the 0th element of array’s 0th element.


#3

If you have myArr = [[‘test’, ‘dog’],[2, ‘cat’], 2, 5, 7, 9] then myArr[8] would return undefined because myArr only has 6 elements.

myArr[0] // ['test', 'dog']
myArr[1] // [2, 'cat']
myArr[2] // 2
myArr[3] // 5
myArr[4] // 7
myArr[5] // 9

#4

Thank you, this was clear and concise.