vote up 1 vote down star

I want to get a value from a variable then use that as the name for another variable. I got something like this:

var eval(BodyWeight[i]["ExerciseTitle"]) = BodyWeight[i]["ExerciseVideo"]; 

This is giving me an error, 'missing ; before statement'.

Any ideas?

flag

40% accept rate
1  
I am pretty sure you can't make dynamic variable names in javascript. What are you trying to achieve so that we can help you with a solution to your larger problem? – Keith Rousseau Feb 6 at 3:57
Sure you can. Variables are properties, and dynamic property names can be used with square bracket property syntax. – Jonathon Feb 6 at 4:07
@Jon: Global variables are properties of window in web browsers, but not necessarily elsewhere. Local variables aren't properties. – outis Feb 6 at 4:10
@outis I could be mistaken, but they are properties of the enclosing object (functions or otherwise). – Jonathon Feb 6 at 4:13
Basically I need to associate variables in a meaningful way. Right now I have an object array but no way to get variables from it except from the index. I don't know if this makes sense. – mike Feb 6 at 4:15
show 4 more comments

3 Answers

vote up 1 vote down

It will be easier if you specify the part you currently have enclosed in eval as a property.

var myvar = {};
myvar[BodyWeight[i]["ExerciseTitle"]] = BodyWeight[i]["ExerciseVideo"];

No evil eval necessary.

link|flag
vote up 1 vote down

While eval will give you a form of variable variables, it's messy and potentially leads to syntax errors:

try {
    eval('var ' + BodyWeight[i]["ExerciseTitle"] + ' = BodyWeight[i].ExerciseVideo');
} catch () {
    // what to do here if BodyWeight[i]["ExerciseTitle"] isn't a valid variabe name?
}

Better to use object properties rather than local variables.

thing[BodyWeight[i].ExerciseTitle] = BodyWeight[i].ExerciseVideo;
link|flag
This is what I got so far: eval('var ' + BodyWeight[i]["ExerciseTitle"] + ' = ' + BodyWeight[i]["ExerciseVideo"]); Except it's giving me an error 'unexpected end of XML entity'. Any ideas =/ ? – mike Feb 6 at 8:06
That's basically the syntax problem I mentioned. The difference between your code and mine is significant. In this case, eval leads you down a dark path. Use object properties instead. – outis Feb 6 at 11:04
vote up 0 vote down

If I understand what you are hoping to accomplish:

var eval(BodyWeight[i]["ExerciseTitle"]) = BodyWeight[i]["ExerciseVideo"]; 

//to try and get
var BodyWeight4ExerciseTitle = BodyWeight[i]["ExerciseVideo"];
              ^-//guessing this is an iterator

To accomplish this, just do:

var key = 'BodyWeight' + i + 'ExerciseTitle';
window[key] = BodyWeight[i]["ExerciseVideo"];

//now you have a global variable "BodyWeight4ExerciseTitle"
link|flag
Actually, I modified this into this: var key = BodyWeight[i]['ExerciseTitle']; window[key] = BodyWeight[i]["ExerciseVideo"]; And it worked! So thanks! – mike Feb 6 at 8:08
Except that globals are evil (c2.com/cgi/wiki?GlobalVariablesAreBad, google.com/search?q=globals+are+evil). Unless you have a compelling reason to use globals (which I don't see for this problem), use local variables or object properties instead. – outis Feb 6 at 11:08
@outis - true... I wasn't sure how the OP needed this variable so sticking it in the window was a simple option... but it could just have easily been stuck in another object. – scunliffe Feb 6 at 12:15

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.