Nested Arrays need help

Nested Arrays need help
0

#1

**Tell us what’s happeniW

What am I doing wrong on this exercise?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 myPlants[2].secondTree [2]; // "pine"
// Setup
var myPlants = [
  { 
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }  
];

// Only change code below this line

var secondTree = ""; // Change this line

Your browser information:

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

Link to the challenge:


#2
  • Remember that arrays use zero-based indices, that is they start counting from 0. If you start from 0, what’s the second index?

  • secondTree doesn’t exist. What properties do you see in each object that you can access?


#3

Hello There,
If you want to access an array, you have to follow the indexing rules. Any Array Index starts with 0.
Suppose this is my custom array:

var users = [
    {
       name: 'test1',
       active: true,
       learningwebsite :[ 'StackOverflow', 'Google', 'youtube' ]
    },
    {
       name: 'test2',
       active: false,
       learningwebsite :[ 'Test', 'Test Site', 'Test Site 3' ]
    }
];

Suppose If I want to access the learning website area and console or print youtube from the list my sample code would be

users[0].learningwebsite[2];

So in your case you are doing wrong indexing. what I mean is that this

myPlants['your desire index'].list['your desire index']

In here, solution will be

var secondTree = myPlants[1].list[2];
// output is pine

If you still feel confused about arrays , I think this will come handy:
Javascript Arrays