Skip to content

Instantly share code, notes, and snippets.

@adamtomat
Created September 22, 2014 09:22
Show Gist options
  • Save adamtomat/5719b9c87f10ad006ca7 to your computer and use it in GitHub Desktop.
Save adamtomat/5719b9c87f10ad006ca7 to your computer and use it in GitHub Desktop.
Get largest value from a map in Sass using max()
// Sass 3.3
// This function can be used to get the largest number from a nested map. Currently only supports 1 sub-map.
// Uses max() to get the biggest number in a map
// See max() docs for reference: http://sass-lang.com/documentation/Sass/Script/Functions.html#max-instance_method
@function get-max($map, $key) {
$list: ();
// Loop through each item in the map
@each $map-key, $sub-map in $map {
// Add the desired value to our list, from the sub-map
$list: append($list, map-get($sub-map, $key), comma);
}
// Get the largest number from our list
@return max($list...);
}
$example: (
foo: (
height: 190px,
width: 200px
),
bar: (
height: 240px,
width: 100px
),
foobar: (
height: 230px,
width: 300px
)
);
$tallest: get-max($example, height); // Returns 240px
$widest: get-max($example, width); // Returns 300px
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment