Accessing Nested Arrays.... it's shows an error "1" can't defined here. How can i solve this?

Accessing Nested Arrays.... it's shows an error "1" can't defined here. How can i solve this?
0

#1

Tell us what’s happening:

Your code so far


// Setup
var myPlants = [
  { 
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }  
];

// Only change code below this line

var secondTree = myPlants[1].trees[1]; // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-nested-arrays


#2

You’re on the right track…and I think it’s probably just a simple mistake. trees is actually the value of type and you need to access the value inside the list array. Look carefully at the code from the example in the lesson:

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"

You’ll see here that you access ‘Fluffy’ by names[1] instead of dog or animalType. So to solve this one, you need to access the second tree by list[1] instead of trees.

Hope that makes sense! :slight_smile:


#3

thank you :kissing_smiling_eyes:


#4

No problem! Have a good day!