我正在尝试创建一个已经有一些标签的数组。我想循环浏览购物车中的每个产品并将标签添加到数组中。{{tag }} 部分正在工作,但它没有被分配给数组。
{% assign finalTaglist = "apples, oranges, peaches" | split: ", " %}
{% for item in cart.items %}
<p>
{% for tag in item.product.tags %}
{{tag}}
{% assign finalTaglist = finalTaglist | concat: tag %}
{% endfor%}
</p>
{% endfor%}
<p>Final Tag List : {{finalTaglist}}</p>`
Run Code Online (Sandbox Code Playgroud)
`
小智 7
concat
用于连接数组,但您的代码试图将字符串添加到数组中,因此它不起作用。
从空字符串开始,然后使用append
添加到字符串中,不要忘记分隔符。构建完成后,用于split
创建数组,然后您可以append
根据需要创建另一个数组。
沿着这些思路的东西(未经测试,只是即兴发挥,但你明白了..)
{% assign finalTaglist = 'apples,oranges,peaches' | split: ',' %}
{% assign newTagList = '' %}
{% for item in cart.items %}
{% for tag in item.product.tags %}
{% assign newTagList = newTagList | append: ',' | append: tag %}
{% endfor%}
{% endfor%}
{% assign newTagList = newTagList | remove_first: ',' | split: ',' %}
{% assign joinedTagLists = finalTaglist | concat: newTagList %}
Run Code Online (Sandbox Code Playgroud)