Home Sass Map map.keys() Sass map.keys() Definition
Definition and Usage map.keys($map) returns a comma-separated list of all keys in $map, in map order. Load it with @use "sass:map" on Dart Sass 1.23+ . Legacy global name: map-keys.
Official sample. map.keys(("regular": 400, "medium": 500, "bold": 700)) → "regular", "medium", "bold".
Top-level only. Keys inside nested maps are not flattened. Pass the nested map itself—for example map.keys(map.get($theme, colors))—to list those keys.
Syntax
Syntax @use "sass:map";
map.keys($map); //=> comma-separated list
map.keys(("regular": 400, "medium": 500, "bold": 700));
// "regular", "medium", "bold"
@each $name in map.keys($font-weights) {
// use $name + map.get($font-weights, $name)
}
// Legacy global (prefer module form)
// map-keys($map); Pattern: map.keys for names → map.get for values → or loop the map directly with @each $key, $value in $map.
Code
SCSS Examples Examples target Dart Sass 1.23+ with @use "sass:map". Open View Compiled CSS for verified output (matches official Sass docs samples).
1. Basic 2. @each utilities 3. Nested maps 4. Count keys 5. Empty map Example 1 — List Keys (Official Docs) Pull every key from a font-weight map.
@use "sass:map";
@use "sass:meta";
@use "sass:list";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
.basic {
--keys: #{meta.inspect(map.keys($font-weights))};
--first: #{list.nth(map.keys($font-weights), 1)};
--sep: #{list.separator(map.keys($font-weights))};
} .basic {
--keys: "regular", "medium", "bold";
--first: regular;
--sep: comma;
} How It Works Three keys come back as a comma list.Order matches the original map.Separator is comma — confirmed with list.separator.Matches the official Sass docs sample.Example 2 — Utility Classes with @each Build a class per key, then read each value with map.get.
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
@each $name in map.keys($font-weights) {
.fw-#{$name} {
font-weight: map.get($font-weights, $name);
}
} .fw-regular {
font-weight: 400;
}
.fw-medium {
font-weight: 500;
}
.fw-bold {
font-weight: 700;
} How It Works map.keys supplies the loop names.Interpolation builds selectors like .fw-medium.map.get fetches the matching weight.Alternative: @each $name, $weight in $font-weights skips the extra get.Example 3 — Keys of a Nested Map List top-level groups, then keys inside one group.
@use "sass:map";
@use "sass:meta";
$theme: (
colors: (primary: navy, accent: teal),
space: (sm: 8px, md: 16px)
);
.nested {
--top: #{meta.inspect(map.keys($theme))};
--colors: #{meta.inspect(map.keys(map.get($theme, colors)))};
} .nested {
--top: colors, space;
--colors: primary, accent;
} How It Works Top-level keys are colors and space only.Nested color keys need a second map.keys on that submap.No automatic flatten across nesting levels.Useful when documenting or validating token groups.Example 4 — Count Map Entries Combine map.keys with list.length.
@use "sass:map";
@use "sass:list";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
.count {
--entries: #{list.length(map.keys($font-weights))};
--also: #{list.length($font-weights)};
} .count {
--entries: 3;
--also: 3;
} How It Works list.length(map.keys(...)) counts keys.Maps also count as lists of pairs, so list.length($map) matches.Either form works ; keys is clearer when you already need the names.Handy for debug output and validation.Example 5 — Empty Map An empty map yields an empty key list.
@use "sass:map";
@use "sass:meta";
@use "sass:list";
.empty {
--keys: #{meta.inspect(map.keys(()))};
--len: #{list.length(map.keys(()))};
} .empty {
--keys: ();
--len: 0;
} How It Works Empty map → empty list ().Length is 0 .Safe to loop — @each simply emits nothing.Guard with length if empty maps should warn or error. Edge Cases & Pitfalls Most map.keys bugs are expecting nested keys, or forgetting the result is a list.
nested No deep key dump map.keys($theme) will not include primary. Call keys on the nested map you care about.
type Result is a list Do not call map.get on the keys list. Use list helpers or @each.
loop Keys alone are not values After @each $k in map.keys($m), still call map.get($m, $k)—or destructure the map directly.
legacy Prefer map.keys over map-keys Global map-keys still works but is deprecated toward Dart Sass 3.0. Use the module API.
Toolchain
Sass Support Prefer the module form with a current Dart Sass release.
API Status map.keys()Dart Sass 1.23+ via @use "sass:map" Global map-keys() Works; deprecated toward Sass 3.0 Return type Comma-separated list of keys Empty map Empty list ()
Compile with the Dart Sass CLI .
Remember
Key Takeaways Returns every key as a comma-separated list in map order.
Top-level only — nest with map.get first for deeper keys.
Great with @each for utility class generation.
Prefer map.keys over deprecated global map-keys.
One line: when you need the names in a map, call map.keys($map).
Practice
Compile Notes Practical checklist:
1. @use "sass:map"
2. Use map.keys for names; map.values for values
3. Nested keys: map.keys(map.get($map, $group))
4. Loop: @each $k in map.keys($m) { map.get($m, $k) }
5. Avoid global map-keys in new code Frequently Asked Questions What does Sass map.keys() do? map.keys($map) returns a comma-separated list of all keys in the map, in the same order as the map. It does not return nested keys from child maps—only the top level of the map you pass.
How is map.keys different from map.values? map.keys returns the names (keys). map.values returns the stored values in the same order. Use keys when you need names for @each or selectors; use values when you only need the numbers/colors.
Is map-keys still valid? Yes. map-keys($map) is the older global name, but Dart Sass deprecates global built-ins toward Sass 3.0. Prefer map.keys after @use "sass:map".
Can I get nested keys in one call? No. map.keys only lists keys of the map you pass. To list keys inside a nested map, first get that nested map: map.keys(map.get($theme, colors)).
What does an empty map return? map.keys(()) returns an empty list (). list.length of that list is 0.
How do I loop over keys? Use @each $key in map.keys($map) { ... } and often map.get($map, $key) inside the loop to read each value. You can also @each $key, $value in $map directly.
Next: Sass map.merge() Combine two maps—or merge into a nested path.
map.merge() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion