address array by variable variable

Discussion in 'Javascript' started by mps@webmind.nl, Jul 19, 2004.

  1. Guest

    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
     
    , Jul 19, 2004
    #1
    1. Advertising

  2. > 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
     
    Vincent van Beveren, Jul 19, 2004
    #2
    1. Advertising

  3. Ivo Guest

    "" 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
     
    Ivo, Jul 19, 2004
    #3
  4. Lee Guest

    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.
     
    Lee, Jul 19, 2004
    #4
  5. lallous Guest

    Hello

    ""
    <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> wrote in
    message news:40fbc710$0$78439$4all.nl...
    > 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] ...

    --
    Elias
     
    lallous, Jul 19, 2004
    #5
  6. Quarco Guest

    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" <> schreef in bericht
    news:40fbcac4$0$62721$...
    > "" 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
    >
    >
     
    Quarco, Jul 19, 2004
    #6
  7. Quarco Guest

    Hey,

    Great..
    It works!!

    Thanx all for your time!!

    Regards,
    Marco


    "lallous" <> schreef in bericht
    news:...
    > Hello
    >
    > ""
    > <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> wrote

    in
    > message news:40fbc710$0$78439$4all.nl...
    > > 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] ...
    >
    > --
    > Elias
    >
    >
     
    Quarco, Jul 19, 2004
    #7
  8. "Quarco" <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:

    > 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" <> schreef in bericht
    > news:40fbcac4$0$62721$...


    Please don't top post.

    Regards
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 19, 2004
    #8
  9. "Quarco" <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:

    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];
    ...
    }
    ...
    ---

    Good luck.
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 19, 2004
    #9
  10. Quarco Guest

    Hmmm ..

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

    Thanx a lot!

    Marco

    "Lasse Reichstein Nielsen" <> schreef in bericht
    news:...
    > "Quarco"

    <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:
    >
    > 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];
    > ...
    > }
    > ...
    > ---
    >
    > Good luck.
    > /L
    > --
    > Lasse Reichstein Nielsen -
    > DHTML Death Colors:

    <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'
     
    Quarco, Jul 20, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?big5?B?r0W84Q==?=

    Why the address of array equals array.

    =?big5?B?r0W84Q==?=, Mar 14, 2007, in forum: C Programming
    Replies:
    8
    Views:
    437
    =?big5?B?r0W84Q==?=
    Mar 15, 2007
  2. candide
    Replies:
    65
    Views:
    1,442
  3. John Koleszar

    address of static array element as address constant

    John Koleszar, Jul 7, 2008, in forum: C Programming
    Replies:
    7
    Views:
    642
    John Koleszar
    Jul 15, 2008
  4. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,194
    Keith Thompson
    Sep 20, 2011
  5. nmvega
    Replies:
    1
    Views:
    171
Loading...

Share This Page