Make Your JavaScript Objects More Predictable by Creating Maps
The Problem With Objects
You’re deep into your code, and suddenly you get an error: “Cannot read property x of undefined.” Awww, man.
Most of the time, this means debugging — attacking my code with debugger
here, console.log()
there. Most of the time, I get it fixed fast. But there are those days that it takes me hours.
Example Problem
We have an object called car
.
The car object has a set of key-value pairs. We get this information from our API. Normally, you’d get an events
property inside the object that includes the property last_updated
— stating the last time this information has been changed.
When running this code, we get an error.
Uncaught TypeError: Cannot read property 'last_updated' of undefined at <anonymous>:1:14
To prevent the error, we have to check if the property events
is in the object or not.
This way, we don’t have an error if the event
property is not in our object. But I find this code to be not that readable.
The Map Solution
When we turn our object into a Map()
, we get all the clear methods from the map.
You can turn your object into a map with the Object.entries()
method and give it your object as a parameter.
This would be the output of the map.
A Map()
has a range of handy methods we can use to make our code more readable, predictable, and with fewer chances for getting errors.
set('key', 'value')
store a key-value pairget('key')
returns the value of a key if availablehas('key')
returnstrue
orfalse
depending on if the key existsdelete('key')
deletes a key-value pair from the mapclear()
removes all the key-value pairs from the mapsize()
returns the amount of key-value pairs from the map
So if we want to have the information last_updated
from the events
the property, we use the .has('prop')
method. This method returns a boolean based on the existence of the property.
In your console, you’ll see the console logged ‘not available’
without any errors.
As already a few other helpful developers showed in the comments. The Map isn’t maybe the best solution for every problem for making your Objects more predictable.
It turns out that the Reflect API, Optional chaining or even better, TypeScript helps to write JavaScript that is more predictable.
Still, I think it could be an interesting solution 😉
Conclusion
I think using Map()
can make your code more readable, predictable, and less sensitive to errors. With the normal Object
, your code will give errors when a certain property is missing. This will lead to bad user experience, so let's prevent this.
If you want to check out more details about Map()
, please check out the detailed page of MDN web docs.
I hope you learned something new with this piece. Happy coding!
Read more
Frontend Development On A Budget: Raspberry Pi 4
medium.com
5 Tips For Healthier Developers
_Your Body And Brains Need Attention_medium.com
What Tech I Use in 2020 As Developer
_Software, Hardware and other stuff I use during my work_medium.com
JavaScript Concepts You Need Before Starting w/ Frameworks & Libraries
_Don’t start before you are comfortable with them_medium.com