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;
}
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;
}
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."