4

I have a list of variables or variable names stored in an array. I want to use them in a loop, but I don't want to have to use eval(). How do I do this? If I store the values in an array with quotes, I have to use eval() on the right side of any equation to render the value. If I store just the variable name, I thought I'd be storing the actual variable, but it's not working right.

$(data.xml).find('Question').each(function(){
  var answer_1 = $(this).find("Answers_1").text();
  var answer_2 = $(this).find("Answers_2").text();
  var answer_3 = $(this).find("Answers_3").text();
  var answer_4 = $(this).find("Answers_4").text();
  var theID = $(this).find("ID").text();
  var theBody = $(this).find("Body").text();

  var answerArray = new Array();

  answerArray = [answer_1,answer_2,answer_3,answer_4,theID,theBody]

  for(x=0;x<=5;x++) {
    testme = answerArray[x];

    alert("looking for = " + answerArray[x] + ", which is " + testme)
  }
});
1
  • I assume you mean # Answers N. Commented Nov 22, 2010 at 1:33

3 Answers 3

3

You can put the values themselves in an array:

var answers = [
    $(this).find("Answers_1").text(),
    $(this).find("Answers_2").text(),
    $(this).find("Answers_3").text(),
    $(this).find("Answers_4").text()
];

for(x=0;x<=5;x++) {
    alert("looking for = " + x + ", which is " + answers[x])
}

EDIT: Or even

var answers = $(this)
    .find("Answers_1, Answers_2, Answers_3, Answers_4")
    .map(function() { return $(this).text(); })
    .get();

If your answers share a common class, you can change the selector to $(this).find('.AnswerClass').

If you need variable names, you can use an associate array:

var thing = { 
    a: "Hello",
    b: "World"
};

var name = 'a';
alert(thing[name]);
Sign up to request clarification or add additional context in comments.

5 Comments

JavaScript doesn't have real associative arrays though; you would just be using an object as one. This can fail if you have such keys as "__proto" (in Firefox) and other special names.
However, those two code-snippets are not equivalent (just as a sidenote)
@idealmachine, a JavaScript object is an associative array, it has some abilities beyond a basic associative array, but that doesn't make it less a such. I don't see any odd behavior in Firefox resulting from using "__proto" as a key, would you care to elaborate?
@eBusiness: I meant __proto__; sorry about the typo. developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… Try var a = {}; a['__proto__'] = "foo"; alert(a);.
Properties can usually be overloaded, it seems strange that this one can't.
1

This would make it easier to get the array populated.

var answers = new Array();
$("Answers_1, Answers_2, Answers_3, Answers_4", this).text(function(index, currentText) {
  answers[index] = currentText;
});

4 Comments

@SLaks ...wouldn't this be easier to get the array populated.
You can be even cleverer. See my answer.
@SLaks...yeah that one came to me just after I wrote this.
I believe you don't have to supply a jQuery object as the context, just supplying this works just fine.
0

As others have mentioned, if you can put the variables in an array or an object, you will be able to access them more cleanly.

You can, however, access the variables through the window object:

var one = 1;
var two = 2;
var three = 3;

var varName = "one";

alert(window[varName]); // -> equivalent to: alert(one);

Of course, you can assign the varName variable any way like, including while looping through an array.

Comments

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.