I have an object o which I want to desconstruct into pieces a, b, c and d. For now I am using three operations. I wonder if it can be done in one operation.
const o = {
a: {
b: "b-value",
c: {
d: "d-value"
}
}
}
This is how I did it in three operations (could it be done in only one?):
const { a } = o
const { b, c } = a
const { d } = c
Display the output:
console.log( {a,b,c,d} )
>Solution :
Yes, you can do it in a single operation
const { a, a: { b, c, c: { d } } } = o;
which outputs all four variables a, b, c and d
a = {b: 'b-value', c: {d: 'd-value'}}
b = 'b-value'
c = {d: 'd-value'}
d = 'd-value'