Search code examples
javascriptreactjsecmascript-6ecmascript-next

Javascript chained destructuring


const someFunction = ({ a }) => {
  const { b } = a;
  return <div>{b}</div>
}

const obj = { a: { b: 1 } }
someFunction(obj)

Is there a way to chain object destructuring so that in someFunction, we can destructure obj to get b within the parameter instead of having to do a separate const { b } = a in the function body?


Solution

  • You can do it like so:

    const someFunction = ({ a: { b } }) => {
      return b;
    }
    
    const obj = { a: { b: 1 } };
    console.log(someFunction(obj));