Skip to content

Instantly share code, notes, and snippets.

@esr360
Last active October 13, 2016 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esr360/0b640a7c44ce8834160d799084152a03 to your computer and use it in GitHub Desktop.
Save esr360/0b640a7c44ce8834160d799084152a03 to your computer and use it in GitHub Desktop.
/// Map Search
/// Get the first occurance of a key from a nested map
///
/// @author [@esr360](http://twitter.com/esr360)
///
/// @access public
/// @group Tools
///
/// @param {map} $map - the map you would like to search
/// @param {string} $target-key - the key you are searching for
@function map-search($map, $target-key, $target-value: '') {
@each $key, $value in $map {
// Is this our key?
@if $key == $target-key {
$target-value: $value;
} @else {
@if type-of($value) == 'map' {
// Does the new map contain our key?
@if map-has-key($value, $target-key) {
$target-value: map-get($value, $target-key);
} @else {
// if not, recurse the function
$target-value: map-search($value, $target-key, $target-value);
}
}
}
}
@return $target-value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment