2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244cd
2021-02-06 04:42:36 +00:00
title: Accessing Nested Arrays
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
forumTopicId: 16160
2021-01-13 03:31:00 +01:00
dashedName: accessing-nested-arrays
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
As we have seen in earlier examples, objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here is an example of how to access a nested array:
2020-04-29 18:29:13 +08:00
```js
var ourPets = [
{
animalType: "cat",
names: [
"Meowzer",
"Fluffy",
"Kit-Cat"
]
},
{
animalType: "dog",
names: [
"Spot",
"Bowser",
"Frankie"
]
}
];
ourPets[0].names[1]; // "Fluffy"
ourPets[1].names[0]; // "Spot"
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Retrieve the second tree from the variable `myPlants` using object dot and array bracket notation.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`secondTree` should equal "pine".
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(secondTree === 'pine');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your code should use dot and bracket notation to access `myPlants` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
2018-10-10 18:03:03 -04:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
(function(x) {
if(typeof x != 'undefined') {
return "secondTree = " + x;
}
return "secondTree is undefined";
})(secondTree);
```
## --seed-contents--
```js
// Setup
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
// Only change code below this line
var secondTree = ""; // Change this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
```js
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
// Only change code below this line
var secondTree = myPlants[1].list[1];
```