Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to deconstruct this in only one operation?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading