array functions

Array functions available through the object 'array' in scriban.

array.add

array.add <list> <value>

Description

Adds a value to the input list.

Arguments

  • list: The input list
  • value: The value to add at the end of the list

Returns

A new list with the value added

Examples

input Try out

{{ [1, 2, 3] | array.add 4 }}

output

[1, 2, 3, 4]

array.add_range

array.add_range <list1> <list2>

Description

Concatenates two lists.

Arguments

  • list1: The 1st input list
  • list2: The 2nd input list

Returns

The concatenation of the two input lists

Examples

input Try out

{{ [1, 2, 3] | array.add_range [4, 5] }}

output

[1, 2, 3, 4, 5]

array.compact

array.compact <list>

Description

Removes any null values from the input list.

Arguments

  • list: An input list

Returns

Returns a list with null value removed

Examples

input Try out

{{ [1, null, 3] | array.compact }}

output

[1, 3]

array.concat

array.concat <list1> <list2>

Description

Concatenates two lists.

Arguments

  • list1: The 1st input list
  • list2: The 2nd input list

Returns

The concatenation of the two input lists

Examples

input Try out

{{ [1, 2, 3] | array.concat [4, 5] }}

output

[1, 2, 3, 4, 5]

array.cycle

array.cycle <list> <group>?

Description

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.

Arguments

  • list: An input list
  • group: The group used. Default is null

Returns

Returns a list with null value removed

Examples

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.any

array.any <list> <function> <args>

Description

Returns the distinct elements of the input list.

Arguments

  • list: An input list
  • function: The function to apply to each item in the list that returns a boolean.
  • args: The arguments to pass to the function

Returns

A boolean indicating if one of the item in the list satisfied the function.

Examples

input Try out

{{ [" hello", " world", "20"] | array.any @string.contains "20"}}
{{ [" hello", " world", "20"] | array.any @string.contains "30"}}

output

true
false

array.each

array.each <list> <function>

Description

Applies the specified function to each element of the input.

Arguments

  • list: An input list
  • function: The function to apply to each item in the list

Returns

Returns a list with each item being transformed by the function.

Examples

input Try out

{{ [" a", " 5", "6 "] | array.each @string.strip }}

output

["a", "5", "6"]

array.filter

array.filter <list> <function>

Description

Filters the input list according the supplied filter function.

Arguments

  • list: An input list
  • function: The function used to test each elemement of the list

Returns

Returns a new list which contains only those elements which match the filter function.

Examples

input Try out

{{["", "200", "","400"] | array.filter @string.empty}}

output

["", ""]

array.first

array.first <list>

Description

Returns the first element of the input list.

Arguments

  • list: The input list

Returns

The first element of the input list.

Examples

input Try out

{{ [4, 5, 6] | array.first }}

output

4

array.insert_at

array.insert_at <list> <index> <value>

Description

Inserts a value at the specified index in the input list.

Arguments

  • list: The input list
  • index: The index in the list where to insert the element
  • value: The value to insert

Returns

A new list with the element inserted.

Examples

input Try out

{{ ["a", "b", "c"] | array.insert_at 2 "Yo" }}

output

["a", "b", "Yo", "c"]

array.join

array.join <list> <delimiter> <function>?

Description

Joins the element of a list separated by a delimiter string and return the concatenated string.

Arguments

  • list: The input list
  • delimiter: The delimiter string to use to separate elements in the output string
  • function: An optional function that will receive the string representation of the item to join and can transform the text before joining.

Returns

A new list with the element inserted.

Examples

input Try out

{{ [1, 2, 3] | array.join "|" }}

output

1|2|3

array.last

array.last <list>

Description

Returns the last element of the input list.

Arguments

  • list: The input list

Returns

The last element of the input list.

Examples

input Try out

{{ [4, 5, 6] | array.last }}

output

6

array.limit

array.limit <list> <count>

Description

Returns a limited number of elments from the input list

Arguments

  • list: The input list
  • count: The number of elements to return from the input list

Returns

Examples

input Try out

{{ [4, 5, 6] | array.limit 2 }}

output

[4, 5]

array.map

array.map <list> <member>

Description

Accepts an array element's attribute as a parameter and creates an array out of each array element's value.

Arguments

  • list: The input list
  • member: The member to extract the value from

Returns

Examples

input 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.offset

array.offset <list> <index>

Description

Returns the remaining of the list after the specified offset

Arguments

  • list: The input list
  • index: The index of a list to return elements

Returns

Examples

input Try out

{{ [4, 5, 6, 7, 8] | array.offset 2 }}

output

[6, 7, 8]

array.remove_at

array.remove_at <list> <index>

Description

Removes an element at the specified index from the input list

Arguments

  • list: The input list
  • index: The index of a list to return elements

Returns

A new list with the element removed. If index is negative, remove at the end of the list.

Examples

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.reverse

array.reverse <list>

Description

Reverses the input list

Arguments

  • list: The input list

Returns

A new list in reversed order.

Examples

input Try out

{{ [4, 5, 6, 7] | array.reverse }}

output

[7, 6, 5, 4]

array.size

array.size <list>

Description

Returns the number of elements in the input list

Arguments

  • list: The input list

Returns

A number of elements in the input list.

Examples

input Try out

{{ [4, 5, 6] | array.size }}

output

3

array.sort

array.sort <list> <member>?

Description

Sorts the elements of the input list according to the value of each element or the value of the specified member of each element

Arguments

  • list: The input list
  • member: The member name to sort according to its value. Null by default, meaning that the element's value are used instead.

Returns

A list sorted according to the value of each element or the value of the specified member of each element.

Examples

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.uniq

array.uniq <list>

Description

Returns the unique elements of the input list.

Arguments

  • list: The input list

Returns

A list of unique elements of the input list.

Examples

input Try out

{{ [1, 1, 4, 5, 8, 8] | array.uniq }}

output

[1, 4, 5, 8]

array.contains

array.contains <list> <item>

Description

Returns if a list contains a specific item.

Arguments

  • list: The input list
  • item: The input item

Returns

true if item is in list; otherwise false

Examples

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.