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
.
Destructuring is a JavaScript expression that allows us to extract data from arrays, objects, and maps and set them into new, distinct variables. Destructuring allows us to extract multiple properties, or items, from an array at a time.
To destroy the structure of something. To dismantle.
JavaScript Object Destructuring is the syntax for extracting values from an object property and assigning them to a variable. The destructuring is also possible for JavaScript Arrays. By default, the object key name becomes the variable that holds the respective value.
Destructuring can make working with an array return value more concise. In this example, f() returns the values [1, 2] as its output, which can be parsed in a single line with destructuring.
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);
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With