RSS Forums RSS
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 16921 | Replies: 3 | Thread Tools  Display Modes
Reply
Join Date: Aug 2004
Posts: 9
Reputation: Cr4nberry is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Cr4nberry's Avatar
Cr4nberry Cr4nberry is offline Offline
Newbie Poster

Using "variable variables" in Javascript

  #1  
May 9th, 2005
Hi there,

I have a little problem working with JavaScript, I was wondering if i could use some trick to call different variables with a base name. I'll explain it:

var number1 = new Array();
var number2 = new Array();

number1[1] = "one";
number1[2] = "two";
number1[3] = "three";

number2[1] = "four";
number2[2] = "five";
number2[3] = "six";

//can I process that arrays in some way like this?

for(var i=0;i=3;i++) {
document.writeln("number.i[i]");
}

I'm trying to read that array in some "dynamic" way, but WITHOUT using bidimensional arrays, trying to use some kind of "dynamic variables name".

Can this be possible without use bidimensional arrays?

Thanks in advance.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,867
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 119
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Using "variable variables" in Javascript

  #2  
May 10th, 2005
To my knowledge... NO. You could do it with Multidimensional array, but trying to do it the way above would never work (not in javascript, anyway). An option to consider, is to create your own object, with a bunch of properties, and then create an array of those objects (I think you can do that in Javascript). Then you can loop through just 1 array, and access any number of properties of the object. So, you make the object to have, say, a "tag" property. Then you create an array of that object, loop through the array, and access the .tag property. I hope I haven't confused more than I Have helped. If so, let me know, and I'll post an example when I get home.
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Using "variable variables" in Javascript

  #3  
May 12th, 2005
There are a few ways you can operate using arrays in javascript... firstly, you don't need to be constrained to using integer indexes... all arrays in javascript are simply normal javascript objects...

hence there are a few ways you can assign new values:

array_x[0] = "red";
or
array_x[array_x.length] = "red";

or you can use the attribute methods....

array["x"] = "red";
is the same as
array.x = "red";

this might allow you to index in a manner you wish.... of course if it doesn't, you can always use the eval function....

eval("array_"+identifier+"["+ index + "]");

may be more what you are after... eval will return the value of the given expression... of course, you would be using javascript to evaluate javascript, and I have heard of this having some problems.
Reply With Quote  
Join Date: Dec 2008
Posts: 1
Reputation: captainbeef is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
captainbeef captainbeef is offline Offline
Newbie Poster

Re: Using "variable variables" in Javascript

  #4  
10 Days Ago
As an alternative to the eval() function, maybe you could try this:

for(var i=0;i=3;i++) {
arrayName = 'number'+i;
document.writeln("window[arrayName][i]");
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:32 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC