Reputation: 95
This kind of Q&A was exist before but there was anything fit with mine. I want to concatenate arrays in liquid within empty array and array
array1
subject = ''
array2
tsubject = ["appple", "pine appeld"]
array what I want to get
["appple", "pine appeld"]
to combine this I tried
{% 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 %}
but nothing changed, subject was still empty.
I think this is because of concat
, concat
concatenate array shapes like this:
{% assign vegetables = "broccoli, carrots, lettuce, tomatoes" | split: ", " %}
But I want concatenate below type:
{% assign vegetables = ["broccoli", "carrots", "lettuce", "tomatoes"] | split: ", " %}
I'm not sure about reason why they don't work. please help me.
Upvotes: 1
Views: 2082
Reputation: 52819
You're mixing string/array values and filters.
{% assign subject = '' %}
subject : {{ subject | inspect }} "" <== String
{% assign subject = '' | split: '' %}
subject : {{ subject | inspect }} [] <== Array
If you want to make an array from all categories in programming collection, you must concat two arrays, and it can be something like :
{% 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 }}
Upvotes: 3