I am sure this has been answered before, but searching only yields results for passing an actual object as a function arguments, however I would like to destructure an object and use its parameters as input arguments for a function.
I am importing the function foo() from an external library and would like to supply its arguments using an object containing them. Here is foo.js (note that my use case has a lot more input arguments):
export function foo(arg1, arg2) {
doStuff()
}
Here is my code; the following works :
import { foo } from 'foo'
const args = {
arg1: "value1",
arg2: "value2",
}
const { arg1, arg2 } = args
foo(arg1, arg2)
I would like to be a bit more concise and avoid repeating the variable names arg1 and arg2, along the lines of:
import { foo } from 'foo'
const args = {
arg1: "value1",
arg2: "value2",
}
foo(args) // doesn't work, apparently I have to do something more to the args object here
But this code doesn't work (arg1 gets the value of args and arg2 is undefined), I assume that this can be achieved, e.g. using the spread operator somehow, but how?
function bar({ arg1, arg2 }), see MDNfunction fooWrapped({ arg1, arg2 }){ return foo.call(this, arg1, arg2); })