【问题标题】:swig-template testing condition with subdocument带有子文档的 swig-template 测试条件
【发布时间】:2014-02-13 05:40:25
【问题描述】:

我有一个json:

var json = [{
        a: "asdf",
        b: "a",
        c: {1:{z:30,x:20,y:50},2:{z:30,x:50,y:30}}
    },
    {
        a: "fdsa",
        b: "o",
        c: {1:{z:10,x:20,y:50},2:{z:0,x:20,y:30}}
    }
]

我想有一个条件来检查:

  • 如果c 对象中的任何项目zxy 大于30,则显示a 的值

这可能吗?我做了一些研究,但找不到任何答案。 请帮忙!谢谢! 我试过了

{% for c,b in json.c %}

【问题讨论】:

    标签: json node.js mongodb express swig-template


    【解决方案1】:

    您的用例非常复杂,可能在服务器端做得更好,但这里有一种方法可以在 swig 中完成...

    {% for item in json %}
      {% set show = false %}
      {% for set in item.c %}
        {% for k in set %}
          {% if k > 30 %}
            {% set show = true %}
          {% endif %}
        {% endfor %}
      {% endfor %}
      {% if show %}
        {{ item.a }}
      {% endif %}
    {% endfor %}
    

    【讨论】:

    • 对不起,我只想显示为。这将返回所有 Cs
    猜你喜欢
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多