Last active
March 20, 2021 14:17
-
-
Save alexcwatt/714f8d311922b6677863a62cd92fdcac to your computer and use it in GitHub Desktop.
JavaScript destructuring example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solution to destructuring problem here: https://kentcdodds.com/blog/javascript-to-know-for-react | |
const info = { | |
title: 'Once Upon a Time', | |
protagonist: { | |
name: 'Emma Swan', | |
enemies: [ | |
{name: 'Regina Mills', title: 'Evil Queen'}, | |
{name: 'Cora Mills', title: 'Queen of Hearts'}, | |
{name: 'Peter Pan', title: `The boy who wouldn't grow up`}, | |
{name: 'Zelena', title: 'The Wicked Witch'}, | |
], | |
}, | |
} | |
const {title, protagonist: {name: protagonistName, enemies: [,,,{name: enemyName, title: enemyTitle}]}} = info; | |
console.log(`${enemyName} (${enemyTitle}) is an enemy to ${protagonistName} in "${title}"`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment