Pejman
Pejman

Reputation: 2636

ES6, get all destructing parameters in function as one object

I'm trying to create an object from destructing parameters of my function

const myFunc = ({foo,bar,baz},something,{x,y,z}) =>{
  console.log(someObjectThatContainsFooBarBaz); // {foor,bar,baz}
  console.log(someObjectThatContainsXYZ); // {x,y,z}
}

I've tried ({foo,bar,baz}=props)=>... and ({foo,bar,baz}:props)=>... already, but none of these works.

Can someone tell me what should I do about this?

Upvotes: 0

Views: 49

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386550

You need another step for creating a new object.

This is not possible to destructure an object and at the same time create a new one with the destructured variables.

const myFunc = ({ foo, bar, baz }, something, { x, y, z }) => {
    console.log({ foo, bar, baz }); // new object
    console.log({ x, y, z });       // new object
}

Upvotes: 3

Related Questions