Tip 10: Use Objects for Static Key-Value Lookups
Understand how to use JavaScript objects as static key-value collections to enhance code clarity. Explore when objects are preferable over arrays for fixed data and discover best practices for sharing unchanging structured information efficiently.
When to use a key-value collection?
You probably noticed that I love arrays. But they are not appropriate in many situations. As you saw, it is always possible to store any type of information in arrays—they are really are that flexible—but it can make things more confusing than necessary. And you often end up obscuring information more than you communicate it.
What if you had some data and you wanted to conditionally apply some colors
in the UI. You want the data to be red if data was below the threshold, green if
everything is within normal range, and blue if some data was above a
threshold. As usual, some very smart designers with a lot of training picked
the absolute perfect shades of these colors (personally, I can never tell the
difference, but that’s why I don’t design).
You could put ...
What the ...