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
Advertisement
Answer
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)));