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 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2012
    Location
    Calgary, Alberta, Canada
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can I use loop increment number to reference a variable?

    I have a large list of variables (which are arrays), all with the same variable name, but with a number behind it.

    i.e.

    item1
    item2
    item3

    I want to create a loop that will display the item information, based on the incremented number.

    i.e.

    for (i=0;i<10;i++)

    document.write(item1[0])

    However in the above example I would like item1 to be item(i)

    Is there any way to do this in javascript?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    for (i=0; i<10; i++){
    var c = ["item" + i];  // item0 to item9
    document.write(c[0]);
    }
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 10-01-2012 at 09:05 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    I have a large list of variables (which are arrays), all with the same variable name, but with a number behind it.
    item1
    item2
    item3
    WHY?

    Why not have an ARRAY OF ARRAYS:

    So you would have (for example)
    Code:
    item[1][0], item[1][1], item[1][2], ...
    and
    item[2][0], item[2][1], item[2][2], ...
    and
    item[3][0], item[3][1], item[3][2], ...
    and so on
    Then there's no need for the (admittedly cute) trick that Philip had to pull.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    for (i=0; i<10; i++){
    var c = ["item" + i];  // item0 to item9
    document.write(c[0]);
    }
    That assigns a single-element array holding a string. [] notation must be applied to a parent property, like window in the case of global variables.

    All properties of my advice will remain unchanged; I don't drive a Transit.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    while we're on the subject of cute...

    Code:
    <body>
    <script>
    var item1=[1,2,3];
    var item2=[7,14,21];
    var item3=[18,36,54];
    
    str="";
    for (i=1;i<4;i++){
    str+="document.write(item"+i+"[0]+'<br>');"
    }
    
    var s = document.createElement('script');
    s.text=str;
    document.body.appendChild(s);
    
    </script>

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    <script type = "text/javascript">
    
    var item1=[1,2,3];
    var item2=[7,14,21];
    var item3=[18,36,54];
    
    var numArrays = 3;  // number of arrays item[x]
    
    function displayArrayValues() {
    for (var i=1; i<=numArrays; i++) {
    var a = window["item"+i];  // array name
    for (var j=0; j<a.length; j++) { 
    document.write(a[j] + "<br>");
    }
    document.write("<br>");
    }
    
    }
    
    displayArrayValues();
    
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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