1

我想从嵌套 for 循环中的最后一个对象中删除逗号:

[{% for item1 in articles.results.entities %}
    {% for item2 in articles1.results.entities %}
        {% if item1.knowledgearticleid != item2.knowledgearticleid %}
            {
            "Id":"{{item1.knowledgearticleid}}",
            "Title":"{{item1.title}}",
            "Articlepublicumber":"{{item1.articlepublicnumber}}",
            "Description":"{{item1.description}}"            
            }
           
        {% endif %}       
       
    {% endfor %}
    {% unless forloop.last %},{% endunless %}
{% endfor %}]

但这不起作用;它给了我重复的逗号:

[
  {
    "Id": "ddcb41c6-1f33-ea11-a813-000d3a3be5cf",
    "Title": "1 Test1",
    "Articlepublicumber": "KA-01992",
    "Description": "Test1"
  },,
  {
    "Id": "9564dc21-9df6-414b-ab99-da4ba534fd83",
    "Title": "Test2",
    "Articlepublicumber": "KA-03363",
    "Description": "Test2"
  }
]
4

1 回答 1

0

在此处讨论的 Shopify 液体片段方面存在相同的问题,这是任何语言连接的常见情况。

对于每个接受的迭代,添加一个逗号,然后是“需要的项目”。除非这是第一次接受的迭代,否则不要添加逗号。

我只是设法修改您的代码,但最终测试它。但你的想法对吗?

[{% assign list_items = "" %}
  {% for item1 in articles.results.entities %}
    {% for item2 in articles1.results.entities %}
        {% if item1.knowledgearticleid != item2.knowledgearticleid %}

         {% unless list_items == "" %}

          {% assign list_items = "zzz" %}

            {
            "Id":"{{item1.knowledgearticleid}}",
            "Title":"{{item1.title}}",
            "Articlepublicumber":"{{item1.articlepublicnumber}}",
            "Description":"{{item1.description}}"            
            }
          {% endunless %}

          {% unless list_items == "zzz" %}

            ,{
            "Id":"{{item1.knowledgearticleid}}",
            "Title":"{{item1.title}}",
            "Articlepublicumber":"{{item1.articlepublicnumber}}",
            "Description":"{{item1.description}}"            
            }
          {% endunless %}

        {% endif %}       
       
    {% endfor %}

{% endfor %}]
于 2020-10-03T16:30:58.890 回答