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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript variable name programming 3

Status
Not open for further replies.

cejones1

Technical User
Joined
Jan 25, 2006
Messages
3
Location
US
I have a javascript variable name defined as lat1. In fact, I have several variables named like such: lat1, lat2, lat3, etc

I want to code a for loop that steps through each lat variable.. and I want to use the for loop index to determine the variable to output...

something like:


var lat1 = "testing";

for (var k = 1; k < 18; k++)
{
var lati = "lat" + k;
var latitude[k] = lati
}

I want latitude[1] to equal "testing"

Is there a syntax I need to use to make lati not text, but actually a pointer to the correct lat1, lat2, etc?

Any help is appreciated

Chris
 
have you tried ?

var lati = eval("lat" + k);
var latitude[k] = lati
 
Sure, something like this will do it...
Code:
var lat1 = "testing";
for (var k = 1; k < 18; k++) {
  var latitude[k] = eval('lat'+k);
}
// latitude[1] is now "testing"
A lot of people steer away from using eval() as it is very slow to complete (apart from being a bit tricky to understand initially).

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Jeff, Simon,

Thank you very much... I figured there was some command that would do what I was trying to accomplish.

You both rock!

Chris
 
Just be careful... Based on the sample code you posted, latitude[1] will NOT be equal to "testing"... at least, once you get out of the for-loop block, since you show declaration of the array var local to the block. Also, that's not how you declare an array variable.

Just a heads-up.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Javascript isn't that strict in variable scope, and the variable declared inside the loop will be visible throughout the whole function.

Lee
 
My goodness, you're right!

Curiouser and curiouser...

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Variables are part of the window collection, so instead of using eval you can also do this:
Code:
var lat1 = "testing";
for (var k = 1; k < 18; k++) {
  var latitude[k] = window['lat'+k];
}
Works in IE, haven't tested in others.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
It works in FF too (v1.5).

Since eval is purported to be very inefficient, I'd use the window collection method. It also has the advantage of being the same syntax as accessing any other collection.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks tsdragon, I will switch to using the window method...
I found that the eval method really slowed down the load of my page... Hopefully window will fix that

Chris
 
Since the parent of an iframe is a window, the same technique is very useful when you want to pass a variable from an iframe to the containing window:

[tt]window.parent['theVar'] = "somevalue";[/tt]

or for calling a function in the parent window:

[tt]window.parent.someFunc("somevalue");[/tt]


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top