【问题标题】:How can I concatenate arrays in liquid如何在液体中连接数组
【发布时间】:2019-02-10 08:57:29
【问题描述】:

以前有这种问答,但有什么适合我的。 我想在空数组和数组中连接液体中的数组

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: ", " %}
我不确定它们为什么不起作用的原因。请帮帮我。

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    您正在混合 字符串/数组 值和过滤器。

    {% 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 }}
    

    【讨论】:

    • 感谢您的回答!现在我得到了分配字符串和数组之间的区别。谢谢它现在有效!现在我将尝试计算是否有相同的类别,以便每个类别中存在多少帖子
    猜你喜欢
    • 2017-03-07
    • 2020-06-06
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    相关资源
    最近更新 更多