0

This function basically should behave like the apply-function in Scheme, just rebuild in Javascript. So I have a given function f with n parameters and an array of length n.

Is there a way to call a function with the values of the array when I don't know how many parameters the function is going to have before hand?

For instance if the function was:

var f = function (x,y,z) {return x+y+z;};

And the Array was [1,2,3], is there a way to call f in a for loop or similar with the values without knowing beforehand how many params f is going to have?

1

1 Answer 1

0
const a = (...args)=>  args.reduce((result,number)=> result+number);

You can use spread operator on a function and it can handle n number of parameters and if you try to use args inside the function it will be in a form of array and the order will be same as that of the parameter passed when calling the function

Sign up to request clarification or add additional context in comments.

1 Comment

This is great but doesn't work in ES3. Function.apply along with the arguments keyword would work in that case, however.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.