1

if I have the following object:

const obj = { 
  a: {
    b: 'val',
  },
};

and I want to destructure both a and b from the object, how would I go about doing it? I know this:

const { a: { b } } = obj;

but this doesn't let me access a. How can I also make a accessible in the code?

Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127

1 Answers1

1

Just include it.

const obj = { 
  a: {
    b: 'val',
  },
};

const { a, a: { b } } = obj;

console.log(a);
console.log(b);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445