Accessing Nested Arrays -- further questions

Accessing Nested Arrays -- further questions
0.0 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].list[1]; // Change this line

ok so I have a question.  Why can I not lookup by a notation like --->  var secondTree = myPlants[1][1][1]; to also access "pine"? 

Your browser information:

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

Link to the challenge:


#2

Because myPlants is an array of 2 objects.

myPlants[1];
// 1 refers to the 2nd element's index, so the 2nd object is being referenced.   

When you try:

myPlants[1][1];
// JS looks for a property called "1" in the 2nd object which does no exist, 
// so this returns undefined

When you go one step further:

myPlants[1][1][1];
// JS looks for a property called "1" on undefined, which results in a TypeError 
// because undefined does not have properties.