| | |
Using "variable variables" in Javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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.
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.
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.
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.
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.
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
As an alternative to the eval() function, maybe you could try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for(var i=0;i=3;i++) { arrayName = 'number'+i; document.writeln("window[arrayName][i]"); }
![]() |
Similar Threads
- "<SCRIPT SRC>" javascript logon help (JavaScript / DHTML / AJAX)
- variables from javascript in asp.net (ASP.NET)
- javascript variable in jsp session (JSP)
- Problem altering "Hidden" Variable (Java)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Form fields loaded with AJAX not submitted in IE
- Next Thread: dependent drop down menus
Views: 23007 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming progressbar prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf






