Results 1 to 5 of 5

Thread: Dynamic Arrays

  1. #1
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamic Arrays

    Does anyone know a way to make a javascript function that will print an array in a similar way to this:
    Code:
    function list(field){
         var nissan=new Array();
         nissan[0]="Quest";
         nissan[1]="Versa";
         nissan[2]="Altima";
     
         var mitsubishi=new Array();
         mitsubishi[0]="Eclipse";
         mitsubishi[1]="Lancer";
         mitsubishi[2]="Outlander";
    
         for(x=0; x<field.length; x++){
               document.write(field[x] + "<br />");
         }
    }
    If I were to run the function list("nissan"), It would, in theory, write:
    Quest
    Versa
    Altima

    But, it doesn't. Instead it writes:
    n
    i
    s
    s
    a
    n

    It thinks the field parameter is a string, which is what it is. But I want it to behave as a variable name, to be able to refer to the nissan and mitsubishi arrays. How can I make the parameter refer to those arrays instead of act as a string?

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    var nissan=new Array();
    nissan[0]="Quest";
    nissan[1]="Versa";
    nissan[2]="Altima";
     
    var mitsubishi=new Array();
    mitsubishi[0]="Eclipse";
    mitsubishi[1]="Lancer";
    mitsubishi[2]="Outlander";
    
    function list(field) {
         for(x=0; x<field.length; x++) document.write(field[x] + "<br />");
    }
    Or better yet

    Code:
    var nissan = ["Quest", "Versa", "Altima"];
     
    var mitsubishi = ["Eclipse", "Lancer", "Outlander"];
    
    function list(field) {
         for(x=0; x<field.length; x++) document.write(field[x] + "<br />");
    }
    If you want to define the arrays inside the function, then you have to use a control statement

    Code:
    function list(field) {
       field = field.toLowerCase();
       var nissan = ["Quest", "Versa", "Altima"];
       var mitsubishi = ["Eclipse", "Lancer", "Outlander"];
       switch(field) {
       case "nissan":
          field = nissan;
          break;
       case "mitsubishi":
          field = mitsubishi;
          break;
       default:
          field = ["Brand not found"];
       }
       for(x=0; x<field.length; x++) document.write(field[x] + "<br />");
    }
    The first two don't take a string, but the var name. The last takes a string.
    Last edited by Trinithis; 11-04-2007 at 06:57 PM.
    Trinithis

  3. #3
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Terrific! Thanks so much! I didn't think the solution would be so simple.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    How 'bout:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
         list.nissan=["Quest","Versa","Altima"];
         list.mitsubishi=["Eclipse","Lancer","Outlander"];
    function list(field){
         for(var x=0; x<list[field].length; x++)
               document.write(list[field][x] + "<br>");
    }
    </script>
    </head>
    <body>
    <script type="text/javascript">
    list('nissan');
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Academically:
    Code:
    <script type="text/javascript">
      var listCars = (function(listsObj) {
        return function(field) {
          for(var i = 0, a = listsObj[field] || [], n = a.length; i < n; ++i)
            document.write(a[i] + "<br>");
        };
      })({
        mitsubishi: ["Eclipse", "Lancer", "Outlander"],
        nissan: ["Quest", "Versa", "Altima"]
      });
    </script>
    However, note that document.write() is no longer a very good idea, and this is an abuse of <br>. Instead, you could have the function return a list element:
    Code:
    <script type="text/javascript">
      var listCars = (function(listsObj) {
        return function(field) {
          for(var i = 0, r = document.createElement("ul"), a = listsObj[field] || [], n = a.length; i < n; ++i)
            r.appendChild(document.createElement("li")).appendChild(document.createTextNode(a[i]));
          return r;
        };
      })({
        mitsubishi: ["Eclipse", "Lancer", "Outlander"],
        nissan: ["Quest", "Versa", "Altima"]
      });
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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