Basic JavaScript - Accessing Nested Arrays

Task Using dot and bracket notation, set the variable secondTree to the second item in the trees list from the myPlants object.

Why cant I use [bracket][bracket] notation here (besides the challenge saying to use [bracket].dot notation)?

I thought [bracket][bracket] notation was valid but my log is returning undefined here…

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

const secondTree = myPlants[1][1];

console.log(secondTree);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Basic JavaScript - Accessing Nested Arrays

Link to the challenge:

myPlants is an array so you can say myPlants[1] to get the 2nd item in the array
But the second item in the array is an object
and that object does not have a property called 1

If you observe the data in myPlants you can see that it is

  1. an array of two objects
  2. each objects consists of keys: type and list
  3. type is string and list is a array of items.

If you want to get second item in the array list whose type is ‘trees’ then

  1. var tempObj = myPlant[1] will return a object which having type=“trees”
  2. Now tempObj having keys: type and list. You should select list by using dot notation tempObj.list and second item using bracket notation tempObj.list[1].

Final output will be myPlant[1].list[1]