-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_nestedArrays.js
29 lines (18 loc) · 953 Bytes
/
11_nestedArrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Nested Arrays
const numberClusters = [
[1,2],[3,4],[5,6]
];
const target = numberClusters[2][1];
console.log(target);
/*
Arrays can store other arrays. When an array contains another array it is known as a nested array. Examine the example below:
const nestedArr = [[1], [2, 3]];
To access the nested arrays we can use bracket notation with the index value:
console.log(nestedArr[1]); // Output: [2, 3]
nestedArr[1] will grab the element in index 1 which is the array [2, 3].
Then, if we wanted to access the elements within the nested array we can chain, or add on, more bracket notation with index values.
console.log(nestedArr[1]); // Output: [2, 3]
console.log(nestedArr[1][0]); // Output: 2
In the second console.log() statement, we have two bracket notations chained to nestedArr. We know that nestedArr[1] is the array [2, 3].
Then to grab the first element from that array, we use nestedArr[1][0] and we get the value of 2.
*/