ES6 deep nested object destructuring

JavascriptEcmascript 6

Javascript Problem Overview


I have an object called this.props which contains

{
 actions: Object,
 dirty: false,
 form: "Statement",
 autofill: function(),
 **statement: Object**
}

statement contains

{
 firstName: "John"
 lastName: "Peter"
 isConfirmed: true
}

I would like to extract statement object and the isConfirmed property in the same line using es6 destructuring

I've tried

const { statement: isConfirmed, isAdmin } = this.props

which I get an error when I do let a = isConfirmed, b = statement

Javascript Solutions


Solution 1 - Javascript

> I would like to extract statement object and the isConfirmed property in the same line

const { statement: { isConfirmed }, statement } = this.props;

That way you get both isConfirmed and the whole statement object.

References:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchefcurry7View Question on Stackoverflow
Solution 1 - JavascriptzerkmsView Answer on Stackoverflow