如何连接液体中的数组

How can I concatenate arrays in liquid

这种问答以前也有过,不过有适合我的。 我想在空数组和 array

中连接 liquid 中的数组

array1
subject = ''
array2
tsubject = ["appple", "pine appeld"]
排列我想要得到的东西
["appple", "pine appeld"]

结合这个我试过了

{% assign subject = '' %}
{% for post in site.programming %}
  {% assign tsubjects = post.categories %}
  {% assign subject=subject | append: tsubjects %}
{% endfor %}
{% assign subject = '' %}
{% for post in site.programming %}
  {% assign tsubjects = post.categories %}
  {% subject=subject | concat: tsubjects %} 
{% endfor %}

但什么都没有改变,主题仍然是空的。 我认为这是因为 concatconcat 像这样连接数组形状:
{% assign vegetables = "broccoli, carrots, lettuce, tomatoes" | split: ", " %}
但我想连接以下类型:
{% assign vegetables = ["broccoli", "carrots", "lettuce", "tomatoes"] | split: ", " %}
我不确定它们不起作用的原因。请帮助我。

您正在混合使用 string/array 值和过滤器。

{% assign subject = '' %}
subject : {{ subject | inspect }} "" <== String

{% assign subject = '' | split: '' %}
subject : {{ subject | inspect }} [] <== Array

如果你想把编程集合中的所有类别组成一个数组,你必须concat两个数组,它可以是这样的:

{% comment %} --- Creates an empty array {% endcomment %}
{% assign subject = '' | split: '' %}

{% comment %} --- Debug output {% endcomment %}
subject : {{ subject | inspect }}

{% for post in site.programming %}
  {% assign tsubjects = post.categories %}

  {% comment %} --- Just to be sure that there is something in post.categories {% endcomment %}
  tsubjects : {{ tsubjects | inspect }}

  {% assign subject=subject | concat: tsubjects %}

  {% comment %} --- Debug output {% endcomment %}
  concat : {{ subject | inspect }}

{% endfor %}

{% comment %} --- Makes sure that values are unique {% endcomment %}
{% assign subject = subject | uniq %}
uniq : {{ subject | inspect }}