简体   繁体   中英

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM