address array by variable variable

M

mps

Suppose.....


arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Thanx in advance,
Marco Snoek
 
V

Vincent van Beveren

Suppose.....
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

I think this is what you are looking for:

eval(ArrayVar+"[0]");

But you can also do the following

arr1 = Array("only", "a", "test");
arr2 = Array("only", "another", "test");

curArray = arr1;
alert(curArray[1]); // displays 'a';

curArray = arr2;
alert(curArray[1]); // displays 'another';

Which feels better.

Good luck,
Vincent
 
I

Ivo

"(e-mail address removed)" wrote
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Not sure what you are trying to do. ArrayVar is a now string variable. If
you remove the quotes from your second line above, so it becomes

ArrayVar = arr;

then ArrayVar will be an array too, and ArrayVar[0] will be "only".
HTH
Ivo
 
L

Lee

SEND said:
Suppose.....


arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

It depends on what you're really trying to do. If you just need
a way to use a variable name for an array, then you can use a
reference, instead of a string holding the name:

var myRef=arr;
alert(myRef[0]);

If you really need to use the string name, then there is probably
a better design that you could use, but one way to do it is:

var arrayGroup = { arr: [ "only", "a", "test" ],
brr: [ "a", "different", "array" ],
crr: [ "one", "more", "example" ]
};
alert(arrayGroup["arr"][0]);


You could also use eval(), but that is inefficient and difficult
to debug.
 
L

lallous

Hello

"(e-mail address removed)"
Suppose.....


arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Yes, you can use eval() as other indicated or you can use something like:

<script>
var x;

array1 = new Array("hello", "world");

x = "array1";

alert(window[x][0]); // <-- hello

</script>


The variables are stored under the window object. You can also do something
like: window["array1"][N] ...
 
Q

Quarco

That is my problem: trying to retrieve an array item with a string
variable-name..
You see, the more elaborate version of my problem is

an array with positions:
positions =
("liggend","staand","staand","liggend","staand","staand","liggend");

an array
liggend[0] = array("value1a","value2a","value3a","value4a","value5a");
liggend[1] = array("value1b","value2b","value3b","value4b","value5b");
....
staand[0] = array("value1c","value2c","value3c","value4c","value5c");
...etc..

Now:
getValue(3) { //For example...
ArrayVar = positions[3]; // in this example ArrayVar = liggend
for(anItem in ArrayVar) {
...... /// walk through the 'liggend-array'..
}
}

I believe the eval function from Vincent does the trick...


Marco



Ivo said:
"(e-mail address removed)" wrote
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined'
instead
of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Not sure what you are trying to do. ArrayVar is a now string variable. If
you remove the quotes from your second line above, so it becomes

ArrayVar = arr;

then ArrayVar will be an array too, and ArrayVar[0] will be "only".
HTH
Ivo
 
Q

Quarco

Hey,

Great..
It works!!

Thanx all for your time!!

Regards,
Marco


lallous said:
Hello

"(e-mail address removed)"
message news:[email protected]...
Suppose.....


arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined'
instead
of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Yes, you can use eval() as other indicated or you can use something like:

<script>
var x;

array1 = new Array("hello", "world");

x = "array1";

alert(window[x][0]); // <-- hello

</script>


The variables are stored under the window object. You can also do something
like: window["array1"][N] ...
 
L

Lasse Reichstein Nielsen

Quarco said:
That is my problem: trying to retrieve an array item with a string
variable-name..

In my experience, you are using a wrong approach to solve the real problem.
I have never seen a case where it was necessary to address a variable
using a string containing its name. Whatever you are trying to do, I'll
almost guarantee I can find a better way to do it :)
an array with positions:
positions =
("liggend","staand","staand","liggend","staand","staand","liggend");

an array
liggend[0] = array("value1a","value2a","value3a","value4a","value5a");
liggend[1] = array("value1b","value2b","value3b","value4b","value5b");
...
staand[0] = array("value1c","value2c","value3c","value4c","value5c");
..etc..

Instead, do:

var c = new Object(); // container object
c.liggend = new Array();
c.staand = new Array();
and then
c.liggend[0] = ...
c.liggend[1] = ...
c.liggend[2] = ...
...
c.staand[0] = ...
c.staand[1] = ...
getValue(3) { //For example...
ArrayVar = positions[3]; // in this example ArrayVar = liggend
for(anItem in ArrayVar) {

function getValue(i) {
var arrayVar = positions;
var arr = c[arrayVar]
for(index in arr) {
var item = arr[index];
// ...
}
...
I believe the eval function from Vincent does the trick...

"eval" is almost never the best solution. It is inefficient and
errorprone, and should be left for the few cases where it can't be
avoided.
"Ivo" <[email protected]> schreef in bericht
news:[email protected]...

Please don't top post.

Regards
/L
 
L

Lasse Reichstein Nielsen

On second thought, this solution is even prettier:

---
var liggend = [
["value1a","value2a","value3a","value4a","value5a"],
["value1b","value2b","value3b","value4b","value5b"],
...
];
var staand = [
["value1c","value2c","value3c","value4c","value5c"],
...
];

var positions = [liggend, staand, staand, liggend, staand, staand, liggend];

function getValue(i) {
var arr = positions;
for (var index in arr) {
var item = arr[index];
...
}
...
 
Q

Quarco

Hmmm ..

Think this is the 'ultimate' solution Lasse... :)

Thanx a lot!

Marco

Lasse Reichstein Nielsen said:
On second thought, this solution is even prettier:

---
var liggend = [
["value1a","value2a","value3a","value4a","value5a"],
["value1b","value2b","value3b","value4b","value5b"],
...
];
var staand = [
["value1c","value2c","value3c","value4c","value5c"],
...
];

var positions = [liggend, staand, staand, liggend, staand, staand, liggend];

function getValue(i) {
var arr = positions;
for (var index in arr) {
var item = arr[index];
...
}
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top