Skip to content

Instantly share code, notes, and snippets.

@fcgomes92
Created August 22, 2019 13:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcgomes92/b85163015fea7923f6a4fd28275d1d2b to your computer and use it in GitHub Desktop.
Save fcgomes92/b85163015fea7923f6a4fd28275d1d2b to your computer and use it in GitHub Desktop.
nestedArrayAndObject.js
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"
@dancingmonkey
Copy link

Thank you I was lost! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment