Created
August 22, 2019 13:12
-
-
Save smokinggoats/b85163015fea7923f6a4fd28275d1d2b to your computer and use it in GitHub Desktop.
nestedArrayAndObject.js
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
function nestedArrayAndObject() { | |
// refactor this to a single line of destructuring... | |
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}, info: {protagonist: {enemies: [,,{enemyTitle, enemyName}]} } = info; // <-- replace the next few `const` lines with this | |
const { | |
title, | |
protagonist: { name: protagonistName }, | |
protagonist: { | |
enemies: [, , , { title: enemyTitle, name: enemyName }] | |
} | |
} = info; // <-- replace the next few `const` lines with this | |
// const title = info.title | |
// const protagonistName = info.protagonist.name | |
// const enemy = info.protagonist.enemies[3] | |
// const enemyTitle = enemy.title | |
// const enemyName = enemy.name | |
return `${enemyName} (${enemyTitle}) is an enemy to ${protagonistName} in "${title}"`; | |
} | |
console.log(nestedArrayAndObject()); // Zelena (The Wicked Witch) is an enemy to Emma Swan in "Once Upon a Time" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you I was lost! 👍