1

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?

9
  • 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? Commented Feb 6, 2010 at 3:57
  • 1
    Sure you can. Variables are properties, and dynamic property names can be used with square bracket property syntax. Commented Feb 6, 2010 at 4:07
  • @Jon: Global variables are properties of window in web browsers, but not necessarily elsewhere. Local variables aren't properties. Commented Feb 6, 2010 at 4:10
  • @outis I could be mistaken, but they are properties of the enclosing object (functions or otherwise). Commented Feb 6, 2010 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. Commented Feb 6, 2010 at 4:15

3 Answers 3

2

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;
2
  • 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 =/ ? Commented Feb 6, 2010 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. Commented Feb 6, 2010 at 11:04
2

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.

0

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"
3
  • Actually, I modified this into this: var key = BodyWeight[i]['ExerciseTitle']; window[key] = BodyWeight[i]["ExerciseVideo"]; And it worked! So thanks! Commented Feb 6, 2010 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. Commented Feb 6, 2010 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. Commented Feb 6, 2010 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.