0

I read and tries everything, but I can't figure out how to do this simple thing.

Basically,

var obj = {
    price: 15
};
var foo = "price";

if(obj.[foo] == 15){
    return true;
}
tery.blargh
  • 405
  • 1
  • 6
  • 23

1 Answers1

-1

You can use bracket notations. But you're also not doing that correctly.

Let's say you had var foo = 'price'; "price" is a key in obj but not its own variable, so you can't just say var foo = price; (an unset variable).

For bracket notation:

if(obj[foo] == 15){
    return true;
} 

In this case you'd be saying "check to see if the price property of object foo is 15."

Jeremy Jackson
  • 2,247
  • 15
  • 24
  • We don't need **yet another** answer to [this question](http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable). *(not my dv)* – T.J. Crowder Aug 05 '17 at 13:47