Array
An array is a collection of data elements.
Concat
Concat (concatenates) tag allows to combine number of arrays. The final array will contain all the elements of the original arrays. It is important to note that the duplicate entries will not be removed from the combined final array unless you use the uniq filter.
{% assign fixed_assets = "buildings,land,computer equipment" |split:"," %}
{% assign intangible_assets = "goodwill,patents,trademarks" |split:"," %}
{% assign assets = fixed_assets |concat:intangible_assets %}
{{ assets |join:", " }}
It can be also applied to collections, as shown in the example below.
{% fori item in custom.items1 %}
{% input item.name %}
{% input item.category %}
{% endfori %}
{% fori item in custom.items2 %}
{% input item.name %}
{% input item.value %}
{% endfori %}
{% assign items = custom.items1 | concat:custom.items2 %}
{% for item in items %}
{{ item.name }}
{{ item.category }}
{{ item.value }}
{% endfor %}
Maximum array size
An array can hold a maximum of 5000 elements.
Split
Will split up a string into an array with the given argument.
{% assign used_acc_numbers = "610000;610015;610023;610045" | split:";" %}
{% for acc in used_acc_numbers %}
{{ acc }}
{% endfor %}
610015
610023
610045
First
Returns the first item of an array.
{% assign used_acc_numbers = "610000;610015;610023;610045" | split:";" %}
{{ used_acc_numbers | first }}
Last
Outputs the last item of an array.
{% assign used_acc_numbers = "610000;610015;610023;610045" | split:";" %}
{{ used_acc_numbers | last }}
Array indices
Alternatively to using the "first" and "last" filters we can use indices.
What's more is that by using indices we are not limited to the first or last item of the array, we can chooses whichever element we want.
When we want to return a particular value from an array based on its “place” in the array we use brackets [] to do so. We attach brackets to the name of the array. The contents of the brackets will be the place (= index) of the value which we wish to return.
0 not 1
Arrays start from 0, not 1. So, if we want to return the third value of an array we will have to input [2]
{% assign my_array = "place_0|place_1|place_2" | split:"|" %}
{{ my_array[2] }}
We build our array with the elements "place0", "place_1" and "place_2". When we want to return "place_2" we will have to input my_array[2]. Keeping in mind that we would indicate this as the third element in _human language but the element with index 2 in array language.
Join
Will join each element of an array with the argument.
{% assign used_acc_numbers = "610000;610015;610023;610045" | split:";" %}
{{ used_acc_numbers | join:" & " }}
Sort
Will sort an array.
{% assign active_directors = "Tim;Joris;Sven" | split:";" %}
{{ active_directors | sort | join:" " }}
Size
Will return the numbers of characters of a given string or give the number of items in an array. This filter can also be used as a [linkto methods]method[endlinkto].
{% assign active_directors = "Tim;Joris;Sven" | split:";" %}
{{ active_directors | size }} or {{ active_directors.size }}
Uniq
Will return unique elements of an array.
{% assign my_accounts = "610000,610023,610045,610000" | split:"," %}
{{ my_accounts | uniq | join:", " }}
Reverse
Reverses the order of the items in an array.
{% assign numbers = "1;2;3;4;5" | split:";" %}
{{ numbers | reverse | join:";" }}
Updated 7 months ago