943,923 Members | Top Members by Rank

Ad:
May 9th, 2005
0

Using "variable variables" in Javascript

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cr4nberry is offline Offline
9 posts
since Aug 2004
May 10th, 2005
0

Re: Using "variable variables" in Javascript

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 12th, 2005
1

Re: Using "variable variables" in Javascript

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.
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Dec 29th, 2008
0

Re: Using "variable variables" in Javascript

As an alternative to the eval() function, maybe you could try this:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for(var i=0;i=3;i++) {
  2. arrayName = 'number'+i;
  3. document.writeln("window[arrayName][i]");
  4. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
captainbeef is offline Offline
1 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Form fields loaded with AJAX not submitted in IE
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: dependent drop down menus





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC