Solved

How to resolve variable that holds name of variable?

Posted on 2015-01-29
3
78 Views
Last Modified: 2015-01-30
I can't figure out how to resolve a variable so that the name of the variable that it holds gets update?
var aa = {}; 
aa.someVar = 'Default Value';
var temp = "aa.someVar"
temp = "New Value":
// which I want to be the same as aa.someVar = "New Value";

Open in new window

0
Comment
Question by:894359
  • 2
3 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 40579353
your code is syntactically wrong.
Use below code:

$(function(){
var aa = {};
aa.someVar = 'Default Value';
var temp = "aa.someVar";
temp = "New Value";
// which I want to be the same as aa.someVar = "New Value";
alert(temp);
});

Open in new window


Same is posted here.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40579653
I think the idea is to alert the original variable which should show the new value was really put in there, not just replicate the results for this specific situation.

Unfortunately the only way I think this is possible (in the exact situation described) is using eval() which is kind of a big no-no. If you want to know why, just google "eval is evil"...

If you are sure the temp variable contains something that will evaluate to a variable reference that you want to set to the new value you can use:
var aa = {};
aa.someVar = 'Default Value';
var temp = "aa.someVar";
eval(temp + ' = "New Value"');
// which I want to be the same as aa.someVar = "New Value";
alert(aa.someVar);

Open in new window


Another option, if you already know which object you want to change (aa) and actually only need to have the property name (someVar) to be a variable, do it like this (much safer code but will only work if you are in that situation of course):
var aa = {};
aa.someVar = 'Default Value';
var temp = "someVar";
aa[temp] = "New Value";
// which I want to be the same as aa.someVar = "New Value";
alert(aa.someVar);

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40579676
PS: another option if you always have a temp variable of type "x.y" and x is a global variable you could use:
window.aa = {}; // for jsFiddle, normally you would use: var aa = {};
$(function(){
aa.someVar = 'Default Value';
var temp = "aa.someVar";
var arraytemp = temp.split('.');
window[arraytemp[0]][arraytemp[1]] = "New Value";
// which I want to be the same as aa.someVar = "New Value";
alert(aa.someVar);
});

Open in new window

A bit more generic (for that "x.y" situation that is ;-) and still better than using eval I guess...

PS2: maybe if you can describe your exact situation it would be possible to advise a completely different approach.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question