html

List and Map Functions in Sass | Explained

Sass, short for Syntactically Awesome Stylesheet, supports the usage of various variables, mixins, functions, etc that in return enhances the capability of the basic CSS language. If we talk about its functions then there are functions for strings, numeric values, lists, maps, etc. The functions that are discussed in this article are list and map functions.

Let’s get started.

List Functions in Sass

Sass list functions are used to manipulate lists such as evaluate list values, join lists, or insert items in a list. However, Sass lists are rigid and do not render any change, therefore, the list functions that fetch a new list do not alter the original one.

Like strings, Sass lists are also 1-based which means that the first list item gets stored at index 1 rather than 0.

1. append(list,value, [separator]) function

This function attaches a single value at the end of the list. A separator can render values such as auto(default), comma, or space.

Example

append ((A, B, C), (D), comma)

Output

A, B, C, D

If space is used.

Example

append ((A, B, C), (D), space)

Output

A B C D

2. index(list,value) function

This function fetches the index position of the value specified in the list.

Example

index ((a,b,c,e,f),3)

Output

c

3. is-bracketed(list) function

This function evaluates if the given list consists of square brackets or not. The result is in the form of true or false.

Example

is-bracketed([1 2 3])

Output

true

If there are no square brackets.

Example

is-bracketed(1 2 3)

Output

false

4. join(list1, list2, [separator, bracketed]) function

This function attaches the second list provided at the end of the first given list. A separator can render values such as auto(default), comma, or space, meanwhile, the bracketed can exhibit auto(default), true, or false values.

Example

join (A B C, D E F)

Output

A B C D E F

When a separator is used.

Example

join ((A B C), (D E F), comma)

Output

A, B, C, D, E, F

When a bracketed is used.

Example

join (A B C, D E F, $bracketed: true)

Output

[A, B, C, D, E, F]

5. length(list) function

For the purpose of fetching the length of the list, this function is utilized.

Example

length(A, B, C, D)

Output

4

6. list-separator(list) function

This function fetches the separator used in the list as a string.

Example

list-separator((A B C))

Output

“space”

7. nth(list,n) function

In order to fetch the nth element specified in the list this function is utilized.

Example

nth (A, B,C, 4)

Output

4

8. set-nth(list,n,value) function

This function gives the value specified to the nth number in the list.

Example

set-nth(A B C,3,y)

Output

A B y

9. zip(lists) function

This function joins all the lists defined into one multidimensional list.

Example

zip(3px 4px 5px, dotted solid dashed, purple yellow black)

Output

3px dotted purple, 4px solid yellow, 5px dashed black

Now that we have learned about list functions, we shall now move to our next topic of discussion.

Map Functions in Sass

Maps in Sass consist of multiple key-value pairs that can be evaluated using map functions. Moreover, you can also use list functions to examine maps in Sass, however, when doing so maps will be considered as lists with two elements.

Just like lists, maps in Sass are rigid and do not exhibit any change; therefore, the map functions that fetch a new map do not alter the original one.

1. map-get(map,key) function

This function fetches the value linked to the key defined in the map.

Example

$color: (black:#000000 ,purple: #6a0dad)

map-get($color,purple)

Output

#6a0dad

2. map-has-key(map,key) function

This function evaluates if the key defined is present in the map or not and provides the result in either true or false.

Example

$color: (black:#000000 ,purple: #6a0dad)

map-has-key($color,yellow)

Output

false

3. map-keys(map) function

This function fetches all the keys in a map.

Example

$color: (black:#000000 ,purple: #6a0dad)

map-keys($color)

Output

black,purple

4. map-merge(map1,map2) function

In order to attach map2 at the end of map1 this function is used.

Example

$color: (black:#000000 ,purple: #6a0dad)

$color1: (pink:#ffc0cb ,brown: #964b00)

map-merge($color, $color1)

Output

black:#000000 ,purple: #6a0dad,pink:#ffc0cb ,brown: #964b00

5. map-remove(map,keys…) function

This function eliminates all the stated keys from the map.

Example

$color: (pink:#ffc0cb ,brown: #964b00,black:#000000)

map-remove($color,brown)

Output

pink:#ffc0cb,black:#000000

6. map-values(map) function

This function fetches all the values present in the map.

Example

$color: (pink:#ffc0cb ,brown: #964b00,black:#000000)

map-values($color)

Output

#ffc0cb, #964b00, #000000

Conclusion

Sass list functions are used to manipulate lists such as evaluate list values, join lists, or insert items in a list. Meanwhile, maps in Sass consist of multiple key-value pairs that can be evaluated using map functions. Both lists and maps are rigid and do not exhibit any change; therefore, any function that fetches a new list or map does not alter the original one. All the functions associated with lists and maps in Sass are explained in this article along with examples.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.