DEV Community

Jonathan Cohen
Jonathan Cohen

Posted on

2 1

Destructuring.....with objects

Last week I posted about destructuring assignment. The post was mainly centered around destructuring with arrays. The only fair thing to do with this post is to talk about how destructuring could also be used on objects. Our example will make use of the summoner Yuna once again and her aeons. Since we need a key-value pair we'll have the aeons be the value and the key will be the temple she received them.

let yuna = {besaid:"Valefor", kilika:"Ifrit", djose:"Ixion", macalania:"Shiva", bevelle:"Bahumaut"}
Enter fullscreen mode Exit fullscreen mode

Already with this, we can access the aeons through dot notation.

yuna.besaid 
return "Valefor"
Enter fullscreen mode Exit fullscreen mode

Since we have only three aeons that deal elemental damage, let's assign the elements to the respective aeon.

const { kilika:fire,  djose:lightning, macalania:ice } = yuna

Enter fullscreen mode Exit fullscreen mode

That line of code sets the proper element variable name seen above to the correct aeon. Fire would return Ifrit, lightning would return Ixion and ice would return Shiva. Take this and try it yourself.

Learning about destructuring has kept me pretty busy but its something I look to make more of a habit as I continue to code.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more