Basic JavaScript - Accessing Nested Arrays

Tell us what’s happening: giving me the error -

secondTree

should equal the string

pine

. Your code should use dot and bracket notation to access

myPlants

Describe your issue in detail here.

  **Your code so far**
const myPlants = [
{
  type: "flowers",
  list: ["rose", "tulip", "dandelion"]
},
{
  type: "trees",
  list: ["fir", "pine", "birch"]
}
];

const secondTree = ["fir", "pine", "birch"];

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Accessing Nested Arrays

Link to the challenge:

Hey there! :wave:

Looks like you just hardcoded the value of myPlants[1].list to the variable secondTree, so two things off.

First the lesson wants you to be

Using dot and bracket notation

As opposed to hard coding. Second they want

the second item in the trees list from the myPlants object.

Or the word "pine" in other words.

honestly, I don’t get it.

Instead of hard coding a value (or literally typing it in) they want you to access it from the myPlants object. If we look myPlants is an array right? So we can use indexes to get each value, just like any other array:

const arr = ['a', 'b', 'c'];
const letter_a = arr[0]; // this will be equal to 'a'
const letter_b = arr[1];  // this will be equal to 'b'

Then with objects, we can get the value of each property using dot notation right?

const obj = {
    property1 : 'a value',
    property2 : 'another value',
    property3 : 'yet another value'
}

const first = obj.property1;  // will be 'a value'
const second = obj.property2; // will be 'another value'

For this task we need to chain these together. myPlants is an array with two objects, we need the one with type "trees" so use indexing to grab the second object:

myPlants[1]

Then we need the list property from that object so we use dot notation:

myPlants[1].list

Now how do we get the second item from that list?

thank you. I appreciate you going through the steps. I got it :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.