array functionsArray functions available through the object 'array' in scriban.
array.addarray.add_rangearray.compactarray.concatarray.cyclearray.anyarray.eacharray.filterarray.firstarray.insert_atarray.joinarray.lastarray.limitarray.maparray.offsetarray.remove_atarray.reversearray.sizearray.sortarray.uniqarray.containsarray.addarray.add <list> <value>
Adds a value to the input list.
list: The input listvalue: The value to add at the end of the listA new list with the value added
input Try out
{{ [1, 2, 3] | array.add 4 }}
output
[1, 2, 3, 4]
array.add_rangearray.add_range <list1> <list2>
Concatenates two lists.
list1: The 1st input listlist2: The 2nd input listThe concatenation of the two input lists
input Try out
{{ [1, 2, 3] | array.add_range [4, 5] }}
output
[1, 2, 3, 4, 5]
array.compactarray.compact <list>
Removes any null values from the input list.
list: An input listReturns a list with null value removed
input Try out
{{ [1, null, 3] | array.compact }}
output
[1, 3]
array.concatarray.concat <list1> <list2>
Concatenates two lists.
list1: The 1st input listlist2: The 2nd input listThe concatenation of the two input lists
input Try out
{{ [1, 2, 3] | array.concat [4, 5] }}
output
[1, 2, 3, 4, 5]
array.cyclearray.cycle <list> <group>?
Loops through a group of strings and outputs them in the order that they were passed as parameters. Each time cycle is called, the next string that was passed as a parameter is output.
list: An input listgroup: The group used. Default is nullReturns a list with null value removed
input Try out
{{ array.cycle ['one', 'two', 'three'] }}
{{ array.cycle ['one', 'two', 'three'] }}
{{ array.cycle ['one', 'two', 'three'] }}
{{ array.cycle ['one', 'two', 'three'] }}
output
one
two
three
one
cycle accepts a parameter called cycle group in cases where you need multiple cycle blocks in one template.
If no name is supplied for the cycle group, then it is assumed that multiple calls with the same parameters are one group.
array.anyarray.any <list> <function> <args>
Returns the distinct elements of the input list.
list: An input listfunction: The function to apply to each item in the list that returns a boolean.args: The arguments to pass to the functionA boolean indicating if one of the item in the list satisfied the function.
input Try out
{{ [" hello", " world", "20"] | array.any @string.contains "20"}}
{{ [" hello", " world", "20"] | array.any @string.contains "30"}}
output
true
false
array.eacharray.each <list> <function>
Applies the specified function to each element of the input.
list: An input listfunction: The function to apply to each item in the listReturns a list with each item being transformed by the function.
input Try out
{{ [" a", " 5", "6 "] | array.each @string.strip }}
output
["a", "5", "6"]
array.filterarray.filter <list> <function>
Filters the input list according the supplied filter function.
list: An input listfunction: The function used to test each elemement of the listReturns a new list which contains only those elements which match the filter function.
input Try out
{{["", "200", "","400"] | array.filter @string.empty}}
output
["", ""]
array.firstarray.first <list>
Returns the first element of the input list.
list: The input listThe first element of the input list.
input Try out
{{ [4, 5, 6] | array.first }}
output
4
array.insert_atarray.insert_at <list> <index> <value>
Inserts a value at the specified index in the input list.
list: The input listindex: The index in the list where to insert the elementvalue: The value to insertA new list with the element inserted.
input Try out
{{ ["a", "b", "c"] | array.insert_at 2 "Yo" }}
output
["a", "b", "Yo", "c"]
array.joinarray.join <list> <delimiter> <function>?
Joins the element of a list separated by a delimiter string and return the concatenated string.
list: The input listdelimiter: The delimiter string to use to separate elements in the output stringfunction: An optional function that will receive the string representation of the item to join and can transform the text before joining.A new list with the element inserted.
input Try out
{{ [1, 2, 3] | array.join "|" }}
output
1|2|3
array.lastarray.last <list>
Returns the last element of the input list.
list: The input listThe last element of the input list.
input Try out
{{ [4, 5, 6] | array.last }}
output
6
array.limitarray.limit <list> <count>
Returns a limited number of elments from the input list
list: The input listcount: The number of elements to return from the input listinput Try out
{{ [4, 5, 6] | array.limit 2 }}
output
[4, 5]
array.maparray.map <list> <member>
Accepts an array element's attribute as a parameter and creates an array out of each array element's value.
list: The input listmember: The member to extract the value frominput Try out
{{
products = [{title: "orange", type: "fruit"}, {title: "computer", type: "electronics"}, {title: "sofa", type: "furniture"}]
products | array.map "type" | array.uniq | array.sort }}
output
["electronics", "fruit", "furniture"]
array.offsetarray.offset <list> <index>
Returns the remaining of the list after the specified offset
list: The input listindex: The index of a list to return elementsinput Try out
{{ [4, 5, 6, 7, 8] | array.offset 2 }}
output
[6, 7, 8]
array.remove_atarray.remove_at <list> <index>
Removes an element at the specified index from the input list
list: The input listindex: The index of a list to return elementsA new list with the element removed. If index is negative, remove at the end of the list.
input Try out
{{ [4, 5, 6, 7, 8] | array.remove_at 2 }}
output
[4, 5, 7, 8]
If the index is negative, removes at the end of the list (notice that we need to put -1 in parenthesis to avoid confusing the parser with a binary - operation):
input Try out
{{ [4, 5, 6, 7, 8] | array.remove_at (-1) }}
output
[4, 5, 6, 7]
array.reversearray.reverse <list>
Reverses the input list
list: The input listA new list in reversed order.
input Try out
{{ [4, 5, 6, 7] | array.reverse }}
output
[7, 6, 5, 4]
array.sizearray.size <list>
Returns the number of elements in the input list
list: The input listA number of elements in the input list.
input Try out
{{ [4, 5, 6] | array.size }}
output
3
array.sortarray.sort <list> <member>?
Sorts the elements of the input list according to the value of each element or the value of the specified member of each element
list: The input listmember: The member name to sort according to its value. Null by default, meaning that the element's value are used instead.A list sorted according to the value of each element or the value of the specified member of each element.
Sorts by element's value:
input Try out
{{ [10, 2, 6] | array.sort }}
output
[2, 6, 10]
Sorts by elements member's value:
input Try out
{{
products = [{title: "orange", type: "fruit"}, {title: "computer", type: "electronics"}, {title: "sofa", type: "furniture"}]
products | array.sort "title" | array.map "title"
}}
output
["computer", "orange", "sofa"]
array.uniqarray.uniq <list>
Returns the unique elements of the input list.
list: The input listA list of unique elements of the input list.
input Try out
{{ [1, 1, 4, 5, 8, 8] | array.uniq }}
output
[1, 4, 5, 8]
array.containsarray.contains <list> <item>
Returns if a list contains a specific item.
list: The input listitem: The input itemtrue if item is in list; otherwise false
input Try out
{{ [1, 2, 3, 4] | array.contains 4 }}
output
true
Note: This document was automatically generated from the source code using
Scriban.DocGen.