FreeCodeCamp.com - Basic JavaSript - Accessing Nested Arrays

Link to FreeCodeCamp

As we have seen in earlier examples, objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays.
Here is an example of how to access a nested array:
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"

Instructions

Retrieve the second tree from the variable myPlants using object dot and array bracket notation.

Sollution

// 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

Komentáře

Populární příspěvky z tohoto blogu

FreeCodeCamp.com - Basic JavaSript - Passing Values to Functions with Arguments

FreeCodeCamp.com - Basic JavaSript - Word Blanks

FreeCodeCamp.com - Basic JavaSript - Understand String Immutability