-2

I need to send parameters dynamically i.e.,(one time two parameters and another time 5 parameters like that). For Example Im having 4 arrays like., a1,a2,a3 and a4. if my condition is 2 then i need to send the any of the 2 arrays as a parameter(example : a1,a2 or a1,a3...). if my condition is 3 means i need to send 3 arrays as a parameter.

Example code:
var a1=[10,20,30,40];
var a2=[11,22,33,44];
var a3=[12,21,15,13];
var a4=[41,25,46,14];

Conditon : arrays==2 then

methodCall(a1,a3); or methodCall(a2,a3);

Please give me any suggestion for get the dynamic parameter.

Hariprasath
  • 828
  • 4
  • 15
  • 41
  • `Function.prototype.apply` is what you need. – elclanrs Dec 30 '13 at 13:07
  • @putvande - That question isn't a duplicate, because it is specifically about how to have an array's elements become individual arguments in a function call. In this question the fact that the arguments are arrays is irrelevant. (Though that question does provide enough information to figure out an answer to this question.) It _is_ a duplicate of [JavaScript variable number of arguments to function](http://stackoverflow.com/questions/2141520/javascript-variable-number-of-arguments-to-function) – nnnnnn Dec 30 '13 at 13:55

3 Answers3

1

Yes, it is possible to send a variable number of arguments in javascript. You have to use the arguments object inside the function to check the number of arguments and retrieve them. This SO question and answer explains it in detail.

Community
  • 1
  • 1
Munim
  • 6,310
  • 2
  • 35
  • 44
1

Use arguments inside the function to get the result

function sayHi() {
  for(var i=0; i<arguments.length; i++) {
    alert("Hi, " + arguments[i])
  }
}
Yogesh
  • 2,198
  • 1
  • 19
  • 28
0

there is arguments keyword available for this

function testme() {
    console.log(arguments.length);
}

testme(1,2,3); // 3
testme(1); //1
Nikita U.
  • 3,540
  • 1
  • 28
  • 37