'Spreading for object as function input arguments
I want to pass in "he", and "she" to function func and output "heshe".
Is there any way to spread the value of object (like array) to make it work?
const func=(a,b)=>(a+b);
const arr=["he","she"];
console.log(func(...arr));//working
const obj1={a:"he", "b":"she"}
console.log(func(...obj1));//not working
Solution 1:[1]
You'll need to use Object.values().
In your example:
const func=(a,b)=>(a+b);
const obj1={a:"he", "b":"she"}
console.log(func(...Object.values(obj1)));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amila Senadheera |
