Nested object destructuring and renaming

Viewed 4206

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.

1 Answers

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

Related