Access the map value from within the map itself
See original GitHub issueHello.
@use "sass:map";
$my-map: ( // what I want...
color: gray, // ...is access to this...
background-color: map.get($my-map, color), // ...from hire.
) // is it possible?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Java: How to Get Keys and Values from a Map - Stack Abuse
In this article, we've gone over a few ways to get keys and values (entries) of a Map in Java. We've covered using...
Read more >Access map value in EL using a variable as key - Stack Overflow
I have a Map in EL as ${map} and I am trying to get the value of it using a key which is...
Read more >Map.prototype.get() - JavaScript - MDN Web Docs - Mozilla
The get() method returns a specified element from a Map object. If the value that is associated to the provided key is an...
Read more >Map and Set - The Modern JavaScript Tutorial
Every map.set call returns the map itself, so we can “chain” the calls: ... So we get a plain object with same key/values...
Read more >Implementing Multidimensional Map in C++ - GeeksforGeeks
Multidimensional maps are used when we want to map a value to a combination of keys. The key can be of any data...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, that’s a common pattern in Sass, and there are multiple solutions outside using self-referential maps.
Modules with
!default
do give users the ability to customize the values dynamically before compilation. If the tool only needs to be configured once per entrypoint/compilation-path, this approach works great.If users need to redefine the variables multiple times per compile, mixins and functions are designed for exactly that: pass in any set of arguments, and re-generate return/output values based on the new input. You can even create a mixin that updates global variables with the
!global
flag, or a function that generates your initial map, and can be called to re-generate that map based on new inputs.If anyone is interested, I’ve used various workarounds for this over the years. I even maintain a tool that is built around exactly this feature: https://www.oddbird.net/accoutrement/. It works by adding a
#key (<functions>)
syntax that is computed whenever the map is called:That gives us both internal map reference, and dynamic calculation when values change. The downsides are a new syntax, and a slightly slower compile. (The latter should be helped by having Sass nested-map functions available now, just need to make the update).
But lately I’ve been moving away from that, and using Sass modules to auto-construct maps from variables. Rather than a
colors
map, I start with a_colors.scss
partial, with variables that can reference each other easily:Then I convert the module-variables to a map on-import, whenever I need it in map-form (for loops, etc):
That covers most of the use-cases for us, without any dependencies. Hope that’s helpful.