140
const a = {
 b: {
  c: 'Hi!'
 }
};

const { b: { c } } = a;

Is it possible rename b in this case? I want get c and also rename b.

leusrox
  • 1,855
  • 3
  • 10
  • 17

2 Answers2

218

You could destructure with a renaming and take the same property for destructuring.

const a = { b: { c: 'Hi!' } };
const { b: formerB, b: { c } } = a;

console.log(formerB)
console.log(c);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
50

You can destructure the same property multiple times, onto different targets:

const { b: {c}, b: d } = a;

This assigns a.b.c to c and a.b to d.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375