Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with array please

    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.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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!");
    }
    }

  • #3
    New Coder
    Join Date
    Oct 2002
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks. how do I access the elements in those arrays?
    like this: (window[array1[ i ]])[x]?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    window[array1[ i ]][x]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •