Reputation: 111
I'm looking for a way to use a string as a lookup in an Object. Here is my code:
$.extend(true, myOptions,{ series: {lines: {show: true} } } );
The lines property could also be one of these:
I have created a form where you can choose between those three options
So the property lines need to be a variable
$.extend(true, myOptions,{ series: {VARIABLE: {show: true} } } );
How do I do that?
Upvotes: 0
Views: 36
Reputation: 123473
Object literals/initialisers don't support using variables as keys. Any identifiers used on the left of a :
will be taken just for their name.
So, you'll have to break it out into multiple steps and use bracket notation to have a VARIABLE
key.
var input = { series: {} };
input.series[VARIABLE] = { show: true };
$.extend(true, myOptions, input);
Upvotes: 1
Reputation: 143119
Your wording is confusing, but if you want to access object properties with the name stored in variable you can do it like obj[var]
.
Upvotes: 0