0

This is driving me insane. If you don't have a common name for the nested string property, how do you access the information?

example:

var fruitStand = {

      'Apples': {
        quantity: 1
      },
      'Pears': {
       quantity: 3
      }
    } 

I know this:

console.log(Object.keys(fruitStand)[0]); // "Apples" 

How do you get to quantity and 1?

I've tried a variety of options (this, adding all kinds of []s) -I'm sure I'm looking right at it, but can't see it. I also can't seem to find documentation to learn how to access nested objects where the nested object is strings (example: fruitStand.______.quantity //# -what do you put in the ____ to loop? )

End goal is to iterate through a the object, but I can't get past how to access all the parts, so I haven't tried to loop yet.

Thank you in advance!

Megan
  • 205
  • 1
  • 3
  • 12
  • To be specific with the duplicate, in your example, it’d be: `var firstKey = Object.keys(fruitStand)[0]; var firstFruit = fruitStand[firstKey]; firstFruit.quantity` – Ry- Jan 24 '17 at 04:22

1 Answers1

1

This should do it: fruitStand[Object.keys(fruitStand)[0]].quantity

amahfouz
  • 2,328
  • 2
  • 16
  • 21