leusrox
leusrox

Reputation: 2375

Destructuring and rename property

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.

Upvotes: 180

Views: 113147

Answers (2)

Bergi
Bergi

Reputation: 664650

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.

Upvotes: 71

Nina Scholz
Nina Scholz

Reputation: 386654

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);

Upvotes: 275

Related Questions