Hi I have 3 javascript arrays that need to be passed in a function when one of the 3 available button is clicked (one button for each array)
<button onclick="call(1)">Array1</button>
<button onclick="call(2)">Array2</button>
<button onclick="call(3)">Array3</button>
JS-
var arr1 = ["s","o","m","e"];
var arr2 =[];
var arr3 =[];
function call(x){
// some code here
if(x==1){
another_func(arr1);
}
else if(x==2){
another_func(arr2);
}
else if(x==3){
another_func(arr3);
}
}
This is working fine but I wish to get rid of that if else loops. Since array names differ by only the numeral 'x' (x is passed by the button when clicked) I tried-
function call(x){
// some code here
var apple="arr"+x;
another_func(apple);
}
But this turned haywire. I know the reason that since javascript is loosly typed language it interpreted latter parameter as a string instead of an array.
Could anybody suggest how should I proceed?
arrayvariables meant to be global variables?another_func(apple)intoif(typeof window[apple] !== 'undefined') { another_func(window[apple]) }