william007
william007

Reputation: 18527

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

Upvotes: 1

Views: 26

Answers (1)

carlosabcs
carlosabcs

Reputation: 545

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

Upvotes: 3

Related Questions