Looping through an array

Discussion in 'Javascript' started by Jarle Kaste, Jul 17, 2003.

  1. Jarle Kaste

    Jarle Kaste Guest

    Take a look at this code:

    var arr = new Array();
    arr["id1"] = 1; // element 0
    arr["id5"] = 2; // element 1
    arr["id47"] = 3; // element 2

    Is it possible to loop through this array without knowing
    the string-id's? Just start out at element 0, and go on
    to 1 and 2 and print out the values?


    --
    Jarle Kaste
    ['Unlock' my address]
     
    Jarle Kaste, Jul 17, 2003
    #1
    1. Advertising

  2. Jarle Kaste

    kaeli Guest

    In article <8SyRa.13810$>,
    enlightened us with...
    > Take a look at this code:
    >
    > var arr = new Array();
    > arr["id1"] = 1; // element 0
    > arr["id5"] = 2; // element 1
    > arr["id47"] = 3; // element 2
    >
    > Is it possible to loop through this array without knowing
    > the string-id's? Just start out at element 0, and go on
    > to 1 and 2 and print out the values?
    >
    >
    >

    <body>
    hi
    <script>
    var arr = new Array();
    arr["id1"] = 1; // element 0
    arr["id5"] = 2; // element 1
    arr["id47"] = 3; // element 2
    for (v in arr)
    alert(arr[v]);

    </script>

    --
    -------------------------------------------------
    ~kaeli~
    There is no justification or rationalization
    for mutilation. Ban declawing as inhumane.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    -------------------------------------------------
     
    kaeli, Jul 17, 2003
    #2
    1. Advertising

  3. "Jarle Kaste" <> writes:

    > Take a look at this code:
    >
    > var arr = new Array();
    > arr["id1"] = 1; // element 0
    > arr["id5"] = 2; // element 1
    > arr["id47"] = 3; // element 2
    >
    > Is it possible to loop through this array without knowing
    > the string-id's? Just start out at element 0, and go on
    > to 1 and 2 and print out the values?


    First of all, there is no need to use an array when you don't use
    integer indices. You might as well use an object, i.e.
    var arr = new Object();

    To iterate through the properties of any object, you can use the
    for(...in...) construct:
    for (var i in arr) {
    ... arr ...
    }

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 17, 2003
    #3
    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. Curts
    Replies:
    1
    Views:
    331
    Lee Fesperman
    Aug 21, 2003
  2. Psybar Phreak
    Replies:
    1
    Views:
    570
    Eric Sosman
    Oct 6, 2003
  3. Replies:
    2
    Views:
    102
  4. Aaron
    Replies:
    2
    Views:
    534
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    283
Loading...

Share This Page