20

I have an array of values:

['a', 'b', 'c', 'd']

and I need to pass these as parameters to a function:

window.myFunction('a', 'b', 'c', 'd');

This would be easier if I could just pass the array/object into the function, but the functions are written by other people or already exist and I cannot change them - they need to be passed as individual parameters, which is what I need solved.

The number of values being passed is not consistent. It may be one, it may be 100.

Again, I cannot affect the functions. They are how they are, and I will always receive an array of values to pass into them.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Randy Hall
  • 7,716
  • 16
  • 73
  • 151

4 Answers4

28

Use the .apply method of the Function object.

window.myFunction.apply(window, ['a','b','c','d']);

The .apply method invokes the function you're calling, but lets you set the function's this value (the first argument) and lets you set its arguments using an Array (the second argument).

So here we kept window as the this value, and we're passing the Array as the individual arguments. The Array members will be distributed as though they were passed as individual arguments, so it's as if you had done this:

window.myFunction('a','b','c','d');
Blue Skies
  • 2,935
  • 16
  • 19
  • It says you posted at the same time, but I'm selecting yours for explanation. Thanks much! – Randy Hall Oct 24 '13 at 16:23
  • Thanks for the answer. However I have a related question, what should be my function definition? I mean how do I check for all those parameters, should i be using a for loop? – codingbbq May 13 '14 at 10:46
  • @noobcode, it's out of scope for this question, but I suppose it's related, so here's a brief answer -- check out JavaScript's "arguments" object. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments That should get you going :-) – Jonathan Gilbert Jun 25 '15 at 14:22
  • What if you don't know how many entries your array has or what they are? (e.g. what if the array is populated with user-entered data) – TylerH Jul 28 '16 at 02:46
5

Try

window.myFunction.apply(window, ['a', 'b', 'c', 'd']);
hazerd
  • 592
  • 4
  • 6
4

In ES6 you can use spread syntax.

As from Docs:

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

Your syntax will be:

var array = ['a', 'b', 'c', 'd'];
myFunction(...array);

Demo:

var array = ['a', 'b', 'c', 'd'];
myFunction(...array);

function myFunction(p1, p2, p3, p4) {
  console.log(p1, p2, p3, p4);
}
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

Array.prototype.map()

var arr = ['a','b','c'];

function echo(x){ 
  console.log(x)
}

function go(){
  arr.map(echo);
}
<button onclick="go()">Test</button>
Community
  • 1
  • 1
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91