问题
I have the following hash:
styles = {
fontSize: {
use: true,
values: {
sm: .875rem,
base: 1rem,
lg: 1.125rem,
}
},
fontWeight: {
use: false,
values: {
light: 300,
normal: 400,
bold: 700,
}
}
}
I would like to create a for in loop that processes the values only if use
is set to true.
I have this code which will loop through the different style types:
for style in styles
items = styles[style]
for item in items
{item}
for property, value in items[item]
{property} value
What I can't figure out is how to test if use
is set to true or false and to then process (or not) the values.
Any ideas on how to do this?
Thanks.
回答1:
Have you considered using a conditional statement, such as an if <value>
statement, inside of the loop?
e.g.
for style in styles
{style}
items = styles[style]
if items.use
for property, value in items.values
{property} value
View demo online here.
来源:https://stackoverflow.com/questions/48927820/conditionally-show-hash-values-in-stylus