Basic JavaScript - Accessing Nested Arrays

Hello! Could someone explain why ourPets[0].names[1] is “Fluffy”?

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

const secondTree = "";
  **Your browser information:**

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

Challenge: Basic JavaScript - Accessing Nested Arrays

Link to the challenge:

To clarify: you are talking about code from instructions?

const ourPets = [
  {
    animalType: "cat",
    names: [
      "Meowzer",
      "Fluffy",
      "Kit-Cat"
    ]
  },
  {
    animalType: "dog",
    names: [
      "Spot",
      "Bowser",
      "Frankie"
    ]
  }
];

ourPets[0].names[1];
ourPets[1].names[0];

ourPets >>> array. It consists of 2 objects.

ourPets[0] >>> first object in this array

names >>> one of the properties of ourPets[0]

ourPets[0].names >>> its an array >>> it consists of 3 strings. these strings have indexes 0, 1, 2 respectively

ourPets[0].names[1] >>> it’s the second string, which is exactly ‘Fluffy’

Then what is the 0 index?

Well, that’s how indexes work.

1st element of array >>> index 0
2nd elem >>> index 1

ourPets[0] >> first element in ourPets array

I mean what is 0 in this case? which one? Because I know what an array is

my_arr = [‘box’, ‘table’, ‘person’]

my_arr[0] >>> 'box'
my_arr[1] >>> 'table'
my_arr[2] >>> 'person'
my_arr = ['box', 'table', 'person']

console.log(my_arr[0]);//box
console.log(my_arr[1]);//table
console.log(my_arr[2]);//person
console.log(my_arr[3]);//undefined

My apologies – I was just showing that I know what an array is. I am saying that I am confused where the array is here in this example because it looks different lol.

  {
    animalType: "cat",
    names: [
      "Meowzer",
      "Fluffy",
      "Kit-Cat"
    ]
  },
  {
    animalType: "dog",
    names: [
      "Spot",
      "Bowser",
      "Frankie"
    ]
  }
];

No worries.
It’s an array, it is just written little differently.

For example ourPets[0]? what is the 0 in this case?

  {
    animalType: "cat",
    names: [
      "Meowzer",
      "Fluffy",
      "Kit-Cat"
    ]

In this case, ourPets[0] it’s an object:

Ah ok so that’s where I was confused — It was stated index 0, but I was trying to find WHERE it was indexing it from. Is it cat?

1 Like

Got it!

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

const secondTree = myPlants[1].list
console.log(secondTree)```

Don’t think so. Cat is a little deeper.
To get cat, we need to write:

ourPets[0].animalType

animalType is property of ourPets[0]. ‘cat’ is va value of this property

Ok so then what is 0 indexing then?

Or maybe I’m not asking the right question? I see ourPets[0]… so I’m asking myself “what is number 1”?

Same with ourPets[1]. What does 1 index here?

well ourPets is an array, right?

This array has two elements: both of them are objects.

ourPets[0]:

ourPets[1]:

Array can consist of different stuff: numbers, strings, objects, other arrays… Here it’s objects. It may seem a little heavy, this code, I get it, but nothing really fancy here.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.