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?
JavaScript
x
8
1
const func=(a,b)=>(a+b);
2
3
const arr=["he","she"];
4
console.log(func(arr));//working
5
6
const obj1={a:"he", "b":"she"}
7
console.log(func(obj1));//not working
8
Advertisement
Answer
You’ll need to use Object.values()
.
In your example:
JavaScript
1
4
1
const func=(a,b)=>(a+b);
2
3
const obj1={a:"he", "b":"she"}
4
console.log(func(Object.values(obj1)));