SASS/SCSS / Maps and Lists in SASS/SCSS
Modifying Maps Dynamically
This tutorial will teach you how to modify the data stored in SASS/SCSS maps dynamically. We will use the map-merge() function and explore its potential use cases.
Section overview
5 resourcesExplains the use of maps and lists for storing and managing data in SASS/SCSS.
Modifying Maps Dynamically in SASS/SCSS
1. Introduction
Goal
In this tutorial, we'll learn how to modify maps dynamically in SASS/SCSS using the map-merge() function, and explore potential use cases.
Learning Outcomes
By the end of this tutorial, you will be able to:
- Understand the concept of SASS/SCSS maps and how to manipulate them.
- Use the map-merge() function to dynamically modify maps.
Prerequisites
This tutorial assumes you have a basic understanding of CSS and a general knowledge of SASS/SCSS.
2. Step-by-Step Guide
Understanding SASS/SCSS Maps
SASS/SCSS maps are like associative arrays. They store key-value pairs where the keys are used to retrieve values.
The map-merge() function
The map-merge() function merges two maps together into a new map. If both maps have the same key, the second map’s value for that key is used.
$map1: (color: blue, size: 10);
$map2: (color: red, shape: circle);
$new-map: map-merge($map1, $map2);
In this example, $new-map will be (color: red, size: 10, shape: circle). The color value from $map2 overrode the color value from $map1.
Best Practices & Tips
- Always ensure that the maps you want to merge have matching data types for their keys.
- Use the
map-merge()function when you want to update a few values in a map without affecting the rest.
3. Code Examples
Example 1: Basic map-merge()
$map1: (color: blue, size: 10);
$map2: (color: red, shape: circle);
$new-map: map-merge($map1, $map2);
// Above code will output
// $new-map: (color: red, size: 10, shape: circle)
In this example, we merge $map1 and $map2 into $new-map. The color value in $new-map comes from $map2.
Example 2: Nested map-merge()
$map1: (color: (background: blue, text: white), size: 10);
$map2: (color: (text: black), shape: circle);
$new-map: map-merge($map1, $map2);
// Above code will output
// $new-map: (color: (text: black), size: 10, shape: circle)
Here we have nested maps. The color key in both maps is another map. The map-merge() function replaces the entire value, so the background key from $map1 is lost in $new-map.
4. Summary
In this tutorial, we learned about SASS/SCSS maps and how to dynamically modify them using the map-merge() function. Remember that map-merge() completely replaces any matching keys in the first map with the value from the second map.
For further learning, look into other map functions in SASS/SCSS, such as map-get(), map-remove(), and map-keys().
5. Practice Exercises
Exercise 1
Given the following maps, merge them into a new map and output the new map.
$map1: (font: Arial, size: 14px);
$map2: (color: black, size: 16px);
Exercise 2
Create a map with nested maps and use map-merge() to modify one of the nested maps.
Solutions
Solution to Exercise 1
$map1: (font: Arial, size: 14px);
$map2: (color: black, size: 16px);
$new-map: map-merge($map1, $map2);
// This will output: (font: Arial, size: 16px, color: black)
Solution to Exercise 2
$map1: (styles: (font: Arial, size: 14px), layout: square);
$map2: (styles: (color: black, size: 16px));
$new-map: map-merge($map1, $map2);
// This will output: (styles: (color: black, size: 16px), layout: square)
For further practice, try creating more complex maps and using other map functions in combination with map-merge().
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article