-1

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} )
MaxP
  • 325
  • 2
  • 14

2 Answers2

4

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'
nip
  • 1,609
  • 10
  • 20
0

in one operation:

const o = {
  a: {
    b: "b-value",
    c: {
      d: "d-value"
    }
  }
}
const {a: {b, c: {d}}} = o;


console.log('{b, d}', {b, d});
Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73