-3

I have this nested object that contains also an array.

result:{
"a": [{ "b": { "c": 1,
               "d": 2,
               "e": 3
              },
        "f": 0
      }]
}

How can I destructure this object using ES6 if I need the value of d?

ade
  • 9
  • 1
  • 4

2 Answers2

4

Object destructuring notation is just like object literal notation, just on the other side of the equals sign. So you write exactly what you'd write to only create the structure necessary for d, and put your d variable/constant name there.

It looks like you're showing us the contents of your object (e.g., it has a property called result), so:

const obj = {
  result: {
    "a": [{
      "b": {
        "c": 1,
        "d": 2,
        "e": 3
      },
      "f": 0
    }]
  }
};
const {
  result: {
    "a": [{
      "b": {
        d
      }
    }]
  }
} = obj;
console.log(d);

I'm not saying I'd use destructuring here. In fact, I probably wouldn't. But you can. :-)

If result was the variable containing the object, just remove that layer:

const obj = {
  "a": [{
    "b": {
      "c": 1,
      "d": 2,
      "e": 3
    },
    "f": 0
  }]
};
const {
  "a": [{
    "b": {
      d
    }
  }]
} = obj;
console.log(d);

Of course, that's taking your question at face value that you want the d from the first entry in a. You can generalize it to get entry n like this (I'm back to assuming result is part of the object):

const obj = {
  result: {
    "a": [
      {
        "b": {
          "c": 1,
          "d": 2,
          "e": 3
        },
        "f": 0
      },
      {
        "b": {
          "c": 1,
          "d": 4, // ***
          "e": 3
        },
        "f": 0
      }
    ]
  }
};
const n = 1; // Which entry in `a` to get
const {
  result: {
    "a": {
      [n]: {
        "b": {
          d
        }
      }
    }
  }
} = obj;
console.log(d);

I'm using object destructuring for a rather than array destructuring, with a computed property name. I can do that because arrays are objects.


Array destructuring notation is just like array literal notation, too. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • If I have to assume, `d` will always have first object's value even if my array has multiple values. – Rajesh Aug 19 '19 at 10:42
  • @Rajesh - I'm basing the above on the question's content, yes. If he needed to pick it out from some other entry, it would look slightly different. – T.J. Crowder Aug 19 '19 at 10:43
  • I was just asking for my understanding. Personally, I do not like such destructuring notation as you loose readability. But given something like this is in my code, I should know what it means. – Rajesh Aug 19 '19 at 10:44
  • 1
    @Rajesh - Yes, that's right, it would take the first one because that's where `{"b": {"d": 2}}` is in the array notation. We can pick any entry from `a`, though. I updated the answer to show how. :-) – T.J. Crowder Aug 19 '19 at 10:47
  • 2
    @Rajesh - For me, destructuring is fantastic if you don't take it too far. For me, the above is too far. :-) – T.J. Crowder Aug 19 '19 at 10:48
  • Exactly! 1 or 2 level maybe fine unless I have to manually read every single letter to understand what is going on. Still thanks! It can be one of interview question... :-p Nonetheless, thanks! – Rajesh Aug 19 '19 at 10:50
-7

You want the value of d?

result.a[0].b.d ?
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jochen
  • 584
  • 3
  • 8