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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using contents of one array as index into another

    I'm having trouble using the contents of an array as an index into another, for example I have an array1 which holds the values 4,5,6. and I want to access array2[4] array2[5] array2[6].

    The code I've been trying to use is something like:

    Code:
    for(i = 1; i < 3; i++)
    {
        alert(array2[ array1[i] ]);
    }
    I've also tried assigning the contents of array1 to another variable 'j' and using that as the index.

    Code:
    for(i = 0; i < 3; i++)
    {
        j = array1[i];
        alert(array2[j]);
    }
    If anybody can help I would greatly appreciate it.

    Thanks.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    How about this


    array1=new Array()
    array1[0]=4
    array1[1]=5
    array1[2]=6

    array2=new Array()
    array2[0]="a"
    array2[1]="b"
    array2[2]="c"
    array2[3]="d"
    array2[4]="e"
    array2[5]="f"
    array2[6]="g"

    for(i = 0; i < 3; i++){
    alert(array2[ array1[i] ])
    }
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply.

    I have a cookie string, which looks something like:

    cookie_name=,1,2,3;

    I split it at the ',' because I just want the numbers 1,2,3 or whatever I've stored in it. I then want to use these numbers as an index into an array.

    so I'm using something like

    numbers = document.cookie.split(',');

    numbers[0] will be cookie_name
    numbers[1] will be 1
    numbers[2] will be 2, etc.

    so I then want to access an array using the numbers which are stored in numbers[1] and >.

    My array is something like:

    var array1 = new Array(10);

    array1[0] = "zero";

    array1[1] = "one";
    ....
    ....

    but If I try to use something like:

    alert(array1[ numbers[1] ]);

    It tells me that the value is undefined


  •  

    Posting Permissions

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