12

Is it possible to rename a variable when destructuring a nested object in JavaScript? Consider the following code:

const obj = {a: 2, b: {c: 3}};
const {a: A, b:{c}} = obj;

How can I rename c in above code like I renamed a to A?

const {a: A, b:{c}: C} = obj

doesn't work.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
darKnight
  • 5,651
  • 13
  • 47
  • 87

1 Answers1

24

The same way you set a new name for A - {c: C}:

const obj = {a: 2, b: {c: 3}};

const {a: A, b:{c: C}} = obj;

console.log(C);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209