I am trying to create a variable that will include another variable.
Example:
var options_id = ...
where 'id' is created dynamically through another variable so that the result could be
var options_1 = ...
var options_2 = ...
etc.
The 'id' is declared dynamically like this:
var id = itemid;
What would be the syntax to include the 'id' variable in the variable
var options_id
?
John Conde
220k100 gold badges464 silver badges504 bronze badges
asked Aug 18, 2012 at 18:11
Yannis
9322 gold badges15 silver badges34 bronze badges
-
You can't specify the name of variable dynamically on declaration instead of this you can crate an array and push the variable value into it.Kundan Singh Chouhan– Kundan Singh Chouhan2012年08月18日 18:13:57 +00:00Commented Aug 18, 2012 at 18:13
-
1why not use an array of options ?MimiEAM– MimiEAM2012年08月18日 18:16:01 +00:00Commented Aug 18, 2012 at 18:16
-
@MimiEAM, we are on same boat :)Kundan Singh Chouhan– Kundan Singh Chouhan2012年08月18日 18:17:12 +00:00Commented Aug 18, 2012 at 18:17
3 Answers 3
Have you thought about using an object instead? You could do:
var id = itemid;
var options = {};
options['id'] = ...;
Then access it using:
options.id
answered Aug 18, 2012 at 18:14
Robin Whittleton
6,3854 gold badges43 silver badges69 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
I suppose this is what your looking for:
window['options_' + id] = ...
answered Aug 18, 2012 at 18:14
CD..
74.4k25 gold badges160 silver badges170 bronze badges
Comments
I think, that there is an Object in JS for that purpose.
For example:
var options = new Object();
var options.id = 'foo';
or if you want to use numeric indices then you can resort to Array
answered Aug 18, 2012 at 18:13
d.k
4,5002 gold badges33 silver badges41 bronze badges
Comments
lang-js