DEV Community

Randy Rivera
Randy Rivera

Posted on

Nested Arrays

Accessing Nested Arrays

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.

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

var secondTree = myPlants[1].list[1]; // will display pine
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)