2018-10-12 15:37:13 -04:00
---
title: Accessing Nested Arrays
---
2019-07-24 00:59:27 -07:00
# Accessing Nested Arrays
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
Accessing elements within an array using bracket notation `[]`
2018-10-12 15:37:13 -04:00
```js
2019-07-24 00:59:27 -07:00
var fruitBasket = ["apple", "banana", "orange", "melon"];
2018-10-12 15:37:13 -04:00
var favoriteFruit = fruitBasket[2];
2019-07-24 00:59:27 -07:00
console.log(favoriteFruit); // 'orange'
2018-10-12 15:37:13 -04:00
```
In this example, our favourite fruit is 'orange' which is at index `2` in the `fruitBasket` array. Using braket notation, we assign index `2` of the `fruitBasket` array to `favoriteFruit` . This makes `favoriteFruit` equal to 'orange'.
2019-07-24 00:59:27 -07:00
### Hint 2
Accessing objects within arrays using braket `[]` and dot `.` notation
2018-10-12 15:37:13 -04:00
```js
var garage = [
{
2019-07-24 00:59:27 -07:00
type: "car",
color: "red",
make: "Ford"
2018-10-12 15:37:13 -04:00
},
{
2019-07-24 00:59:27 -07:00
type: "motorbike",
color: "black",
make: "Yamaha"
2018-10-12 15:37:13 -04:00
},
{
2019-07-24 00:59:27 -07:00
type: "bus",
color: "yellow",
make: "Blue Bird"
2018-10-12 15:37:13 -04:00
}
];
var busColor = garage[2].color; // 'yellow'
```
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
```js
// Setup
var myPlants = [
2019-07-24 00:59:27 -07:00
{
2018-10-12 15:37:13 -04:00
type: "flowers",
2019-07-24 00:59:27 -07:00
list: ["rose", "tulip", "dandelion"]
2018-10-12 15:37:13 -04:00
},
{
type: "trees",
2019-07-24 00:59:27 -07:00
list: ["fir", "pine", "birch"]
}
2018-10-12 15:37:13 -04:00
];
// Only change code below this line
var secondTree = myPlants[1].list[1];
```
2019-07-24 00:59:27 -07:00
< / details >