Skip to main content

Filters for Array Operations with Scriptlets

If you have a text string that is separated with a delimiter, like a comma, you can use the array filter to extract data, sort the list or output the content in a different format.

size

Returns the the number of items in an array or number of characters in a string

{? "{{Your Name}}" | size | prepend: "The name is " | append: " characters long" ?}
// Output: The name is 19 characters long

The size filter works for strings and arrays.

{? "Apples, Oranges, Bananas" | split: ", " | size |  append: " fruits found!" ?}
// Output: 3 fruits found!

split

Converts a string of words into an array. The split filter is commonly used to convert comma-separated values from an input string to an array.

{% assign fruits = "Apple, Orange, Mango" | split: ", " %}
{% for fruit in fruits %}
{? fruit ?}
{% endfor %}

join

Combines the items in an array into a single string.

If you have a Google Sheet containing names of college students and the programming language skill they have, you can send a well-framed email to an authority in your company with the skills of each student.

{? "C++, Java" | split: ", " | join: " and " | prepend: "Alice knows " ?}

The prepend filter adds text to the beginning of the string.

reverse

The reverse filter can be used to reverse the order of items in an array or characters of a string.

{? "i love oranges" | split: "" | reverse | join: "" ?}
// Output: segnaro evol i

The split command converts the string into separate words and the reverse command reverses the order of those words. The join command converts the array items back into a single string.

{? "Apple, Orange, Mango" | split: ", " | reverse | join: ", " ?}

uniq

Removes any duplicate elements in an array. You can use this filter to remove any possible duplicate entries in the answer field.

{? "Apple, Orange, Apple, Mango, Orange" | split: ", " | uniq | join: ", " ?}

First, the string of words is split into separate words using a comma as the separator. The uniq filters removes any second occurrence of the same word, that is Apple and Orange. Then, the join command joins them back into a single string of words.

sort

Sorts items in an array in case-sensitive order.

{? "Aa, abc, Xy, Cd, bc" | split: ", " | sort | join: ", " ?}

// Output: Aa, Cd, Xy, abc, bc

The split filter converts the string into an array, the sort filter arranges these words alphabetically and the join filter converts the array back into a string. All words with letters in uppercase are sorted alphabetically, followed by all words in lowercase.

sort_natural

Sorts items in an array in case-insensitive order.

For instance, if there are multiple winners of a prize and you have their names in a Google Sheet, use the sort_natural filter to sort these names before outputting them in an email message.

{? "Angus, athena, Xavier, Catherine" | split: ", " | sort_natural | join: ", " ?}

first

Returns the first word of a string. For instance, the respondents might have filled up their full names in response to one of the questions in your Google Form asking for their name. You however need their first name only in the greeting.

{? "Angus McDonald" | split: " " | first ?}

The split filter separates the input text into words, using the space as a separator. Next, the first filter returns the first word of the array.

If the input value contains words that are comma-separated, you need to update the split filter to use commas as the separator. For instance, if you have a Google Form for respondents to fill out their preferred subjects for an online course, and want to allot the first preference to the first ten respondents, you can use this scriptlet.

{? "Humanities, English, Business Studies" | split: ", " | first ?}
{? {{Your preferred subjects}} | split: ", " | first ?}

last

Returns the last item of an array. This filter can be used when you want to display the last word in the answer field like the last name of form respondent in an email.

{? "Angus McDonald" | split: " " | last ?}

If you are using these scriptlets with a dynamic field that is populated from a Google Form or a Google Sheet, use the variable field enclosed in double curly braces.

{? "{{Full Name}}" | split: " " | last ?}

concat

Merge two or more arrays. The resulting array contains all the items from the input arrays.

{? "Angus McDonald" | split: " " | last ?}

{% assign girls = "Elizabeth, Maya, Rachel" | split: ", " %}
{% assign boys = "Robert, Angus, Jacob" | split: ", " %}

{% assign students = girls | concat: boys %}

{% for person in students %}
- {? person ?}
{% endfor %}