I am trying to spread an object to use as the parameters of a function. I'm pretty sure I've seen this done, but I cant remember how. I know how to do this with an array, but I need to do it with an object. Also, I can't just turn the object into an array using Object.values(obj)
since in some cases the order of the values won't match the order of the params.
function sum(x, y, z) {
return x + y + z
}
obj = {x: 23, y: 52, z: 12}
sum(...obj) // <--- WHAT DO I PUT HERE?
Using an array, I would just do:
function sum(x, y, z) {
return x + y + z
}
arr = [23, 52, 12]
sum(...arr)
In python, this would be:
def sum(x, y, z):
return x + y + z
dict = {'x': 23, 'y': 52, 'z': 12}
sum(**dict)