...

View Full Version : help with array please



hongr
01-15-2004, 03:01 AM
array1 = new Array("a1","a2","a3");
a1 = new Array("1", "2");
a2 = new Array("1", "2");

I have some arrays here. As you can see, some of the values of array1 match the names of the arrays below. What I want to do is to go through array1, and check to see if the matching array exists.
I tried this, but didnt work.

for (i=0; i<array1.length; i++)
{
if (array1[i].length > 0)
{
alert ("yes!");
}
}

when I did that, it actually got the length of the string "a1", not the array a1. How do I reference the match arrays correctly? thank you.

glenngv
01-15-2004, 04:03 AM
if a1 and a2 are global variables, you can also access them as

window['a1']
window['a2']

so you can check if an element in array1 evaluates to a variable name, with this code:


for (i=0; i<array1.length; i++)
{
if (window[array1[ i ]])
{
alert ("yes!");
}
}

hongr
01-15-2004, 04:34 AM
thanks. how do I access the elements in those arrays?
like this: (window[array1[ i ]])[x]?

glenngv
01-15-2004, 04:48 AM
window[array1[ i ]][x]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum