DEV Community

Jonathan Cohen
Jonathan Cohen

Posted on

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.

Top comments (0)