-3

I want to access the value of my object but with a .var. For example, if I specifiy the full path by myself it's apple = theApples.trees.green but I want to do:

var colorOfApple = "red";
apple = theApples.trees.colorOfApple;

the problem is it tries to access .colorOfApple of the object but I want to have the value of colorOfApple

Jerome
  • 1,162
  • 2
  • 16
  • 32
  • Use bracket notation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors – RaR May 26 '17 at 13:21
  • Sorry for the dup, I didn't found this answer before. I'll give more attention next time – Jerome May 26 '17 at 13:22

1 Answers1

2

This is very simple, just apple = theApples.trees[colorOfApple];

MysterX
  • 2,318
  • 1
  • 12
  • 11