Goodbye nested lists, hello SASS maps!
SASS lists are great, and with multidimensional lists you can get a key-value-like experience, but not completely..
Let’s start out with an example case. We want to have buttons on our website (who doesn’t), but we want them in multiple colors. Off course, lazy programmers we are, we don’t want to define a class for each color. Let’s do that with multidimensional lists.
In the example above we make a list with all the button colors we want. Each item of the list, is a list of two items: the color name (a string) and the color value (Hex Code). Then in the @each loop, we loop through all the colors. For each color we use the nth function to get the first or second item in the list. The first item will be the color name, the second the color value.
In most programming languages, a list or array starts with index 0, in SASS it starts with 1 (which is quite ‘odd’ ;-)). This is important to know when using the nth function.
Now let’s refactor this, using SASS maps:
This will give the exact same result as in the example above, but the code is cleaner. As you can see the list is now a map with key value pair. The key being the color name and the value being the color hex value. Then we loop through the colors just like before, but now we define a placeholder name for the key and the value (in that order). Now we can just use those variables instead of having to get the ‘nth’ item of a nested list.
As you can see a subtle but nice feature! You will need SASS 3.3 to compile this, try it out. I am excited to hear other use cases that you come up with!
- Official Documentation: sass-lang.com/documentation/file.SASS_REFERENCE.html#maps