Destructuring.....with objects
Jonathan Cohen
Posted on May 19, 2021
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"}
Already with this, we can access the aeons through dot notation.
yuna.besaid
return "Valefor"
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
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.
Posted on May 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024