Conditionally Show Hash Values in Stylus

喜欢而已 提交于 2019-12-24 00:59:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!