SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing Array to Function

    I would like to pass an array to a function but how does the program know which array I would like to choose from??
    Lets say I have 3 arrays and I would like to pass array C, to my function.
    I checked the web but they only show if you have ONLY 1 array but NOT for multiple arrays.
    How would I even go about doing this??


    Code:
    var arrA=new Array("fox.com","nbc.com","abc.com", "google.com");
    var arrB=new Array("car","bike","boat", "plane");
    var arrC=new Array("1","2","3", "4", "5", "6", "7", "8", "9");
    
    function display(myArray){
       myArray[1] = "changed";
    }
    
    display(myArray);
    
    document.writeln(myArray[1]);
    thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    When you call the function, you pass the array you want in the parameter:
    Code:
    var arrA=new Array("fox.com","nbc.com","abc.com", "google.com");
    var arrB=new Array("car","bike","boat", "plane");
    var arrC=new Array("1","2","3", "4", "5", "6", "7", "8", "9");
    
    function display(myArray){
       myArray[1] = "changed";
    }
    
    display(arrB);
    
    document.writeln(arrB[1]);

  3. #3
    SitePoint Enthusiast Adam Chrapkowski's Avatar
    Join Date
    Sep 2011
    Location
    Poland
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm unsure that I understand the question well. If you want to pass a few arrays into the function, then just separate their names by comma.
    Code:
    function display(arr1, arr2, arr3) {
      // ...
    }
    
    display(arrA, arrB, arrC);


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
  •