Search code examples
functionsass

Print out sass keys from a map


Similar to this older answer, I'd like to dump out possible values in the map that could be used.

Is there a sugared way in dart-sass to dump out just map's keys?

@function map-keys($map) {
  $keys: ();

  @each $key, $_ in $map {
    $keys: append($keys, $key);
  }

  @return $keys;
}

Solution

  • There is in fact a built in function around map keys. Where given a sass map, map-keys returns a list of the keys.

    map.keys($map)
    map-keys($map) //=> list
    

    Returns a comma-separated list of all the keys in $map.

    @use "sass:map";
    
    $font-weights: ("regular": 400, "medium": 500, "bold": 700);
    
    @debug map.keys($font-weights); // "regular", "medium", "bold"
    

    https://sass-lang.com/documentation/modules/map/#keys