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??

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

Do your array variables global? If you declare them as global (inside script tag but outside all functions), you can use it anyway you want. One way to do it without changing your original data structure is as follows:

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(arrNum, arrIdx){
  if (!isNaN(arrNum) && !isNaN(arrIdx) {  // ensure integer
    if (arrNum==1) { arrA[arrIdx] = "changed" }
    else if (arrNum==2) { arrB[arrIdx] = "changed" }
    else if (arrNum==3) { arrC[arrIdx] = "changed" }
  }
}

display(3, 1);

writeToPage(arrC, 1);  // create another JS function to do the document.write or whatever

Though, I would use hash object if I were you. However, the array is still a global in this case. Then I would use the "key" to call in any function as a passing argument.

var myArr = {}
myArr["A"] = new Array("fox.com","nbc.com","abc.com", "google.com");
myArr["B"] = new Array("car","bike","boat", "plane");
myArr["C"] = new Array("1","2","3", "4", "5", "6", "7", "8", "9");

function update(arrKey, arrIdx){
  if (myArr[arrKey]) {  // ensure that the array key exists
    myArr[arrKey][arrIdx] = "change"
  }
}

alert(myArr["A"])
update("A", 1)
alert(myArr["A"])

You could just pass it by name.

Say you want to change the second entry of arrA (remember that array indexing begins at 0).

In the main program, just feed arrA into the sub-routine "display":

display(arrA);
   
   document.writeln(arrA[1]);

Remember that arrays are considered a reference type, not a primitive type, so they are passed by reference into sub-routines. In other words, the changed arrA array will be returned to the main program after it has been passed into "display". If you intend to use original values of arrA later in the program, you should copy those values first.

Even though you could simply pass the array as reference, it is not recommended because you are creating side-effect to your original variable without explicitly show in your function declaration. That's why I incorporate "index" value to the call if there is any change in the passing reference variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.