Tip 10: Use Objects for Static Key-Value Lookups

In this tip, you’ll learn why objects are the best collection for simple key-value lookups.

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 the hex values in an array, but that doesn’t really communicate much.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy