Hi,
I'm sure this is a JavaScript 101 question, but I have a bit of a problem. I am using a for loop to iterate through an array, and I would like to assign the value of each element to a variable that already exists. I will be addressing the variable using a name appended with a number that will correspond to the counter value in the loop.
So the loop starts, the first element of the array needs to be assigned to a variable eg variable_0. Then on subsequent loops variable_1 is assigned the value of the second element in the array. My problem is I have no idea how to create this variable. If I use i as my counter my idea would be:
But obviously this will not work - it will create a new variable named variable_i.
I can not use concatenation on the lefthand side of the statement, as it is illegal. eg
I can not assign the name to another variable, as the assignment will be to the holder variable:
I am sure there is a way to do this, but my brain is fried and I don't have a programming knowledge. Please help!
I'm sure this is a JavaScript 101 question, but I have a bit of a problem. I am using a for loop to iterate through an array, and I would like to assign the value of each element to a variable that already exists. I will be addressing the variable using a name appended with a number that will correspond to the counter value in the loop.
So the loop starts, the first element of the array needs to be assigned to a variable eg variable_0. Then on subsequent loops variable_1 is assigned the value of the second element in the array. My problem is I have no idea how to create this variable. If I use i as my counter my idea would be:
Code:
variable_i = myArray[i];
But obviously this will not work - it will create a new variable named variable_i.
I can not use concatenation on the lefthand side of the statement, as it is illegal. eg
Code:
variable_ + "i" = myArray[i];
I can not assign the name to another variable, as the assignment will be to the holder variable:
Code:
tempVar = "variable_" + i;
tempVar = myArray[i];
I am sure there is a way to do this, but my brain is fried and I don't have a programming knowledge. Please help!