Converting an array to a function arguments list [duplicate]
Solution 1:
Yes. In current versions of JS you can use:
app[func]( ...args );
Users of ES5 and older will need to use the .apply()
method:
app[func].apply( this, args );
Read up on these methods at MDN:
- .apply()
- spread "..." operator (not to be confused with the related rest "..." parameters operator: it's good to read up on both!)
Solution 2:
A very readable example from another post on similar topic:
var args = [ 'p0', 'p1', 'p2' ];
function call_me (param0, param1, param2 ) {
// ...
}
// Calling the function using the array with apply()
call_me.apply(this, args);
And here a link to the original post that I personally liked for its readability
Solution 3:
app[func].apply(this, args);