Skip to content

Instantly share code, notes, and snippets.

@alexcwatt
Last active March 20, 2021 14:17
Show Gist options
  • Save alexcwatt/714f8d311922b6677863a62cd92fdcac to your computer and use it in GitHub Desktop.
Save alexcwatt/714f8d311922b6677863a62cd92fdcac to your computer and use it in GitHub Desktop.
JavaScript destructuring example
// 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