0

I need to pass some method parameters using a loop. Something like this:

array.forEach(element => {
    // Pseudo Code.
    // Add the parameter value to the method call on each loop.
    myMethod().parameters.append(element)
});


// OR -->
let params;
array.forEach(element => {
    // Pseudo Code.
    params.append(element);
});
myMethod(params)

Is this possible in Typescript/JavaScript?

2
  • Not sure what you are asking. Are you going to invoke myMethod() multiple times with each time having an additional parameter? Commented Jul 8, 2019 at 4:20
  • @unional I don't need to do it like shown above. I have a function callback and I need to add the parameters dynamically. Sometimes I just need to pass a single object into the method I am calling, other times I have multiple parameters I need to pass. For example, myMethod() above may take myMethod(someObject) or it may take myMethod(true, false, "some string"). myMethod() is dynamic and not known. Commented Jul 8, 2019 at 4:25

1 Answer 1

2

All you need is the fn.apply():

myMethod.apply(this, array)

https://www.w3schools.com/js/js_function_apply.asp

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.