Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign variable values using loop

Status
Not open for further replies.

gollyg

Technical User
Oct 24, 2000
131
0
0
AU
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:

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!
 
You really don't need to assign variables to array values. An array is, in fact, a variable by itself. (The whole point of an array is so that you don't have to create a passle of variables.) You can access the different values simply by using an array subscript. Example:

myArray = new Array();
myArray = "Tom, Dick, Harry";

To use these values, you would do this:

myArray[0] (this equals "Tom")
myArray[1] (this equals "Dick")
myArray[2] (this equals "Harry")

Now, to loop through the array with a for loop:

for (i = 0; i <= myArray.length; i++){
what ever needs to be done;}



There's always a better way. The fun is trying to find it!
 
Thanks for the response.

Actually I already have an array of variables, but I need to extract the variables from the array and assign them to an pre-existing objects property.
The objects are named with an _0, _1 etc suffix, so when I loop through the array I want to assign the value of the current array index to the property of the object. To do this I need to identify the object based upon the counter value.

I may be missing something here (a brain perhaps!) but I don't know how to do it.

Thanks again for your help
 
For(i = 0; i <= arrayname.length; i++){
"_"+i = arrayname;}

This may not be the exact syntax but it should be very close. I don't if you are using actual variables or input field names but this should get you on the right track.

There's always a better way. The fun is trying to find it!
 
Thanks tviman,

I'll give it a go

Cheers
 
I think there is a problem with this approach - it is that you can't concatenate strings on the left hand side of an assignment operator - it gives an error. I am sure there must be some standard way of doing this?

Thanks again
 
If you create your objects as window variables like this...
window.object_1 = new myCustomObject();

Then you could loop like this:
Code:
for(var i = 0; i <= arrayname.length; i++){
  window["object_"+i].propertyName = arrayname[i];
}
If you want to leave the objects as they are, you can use eval();
Code:
for(var i = 0; i <= arrayname.length; i++){
  eval("object_"+i+".propertyName=arrayname["+i+"]");
}

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top