Skip to main content

Array functions

count

Counts the number of elements in an array

count(array)

filter

Filters elements of an array using a callback function.

Function Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array.

Arguments:

  • array - the array to be modified
  • callback - a closure function with expression in the body which should be applied to every element in array

In this example datavars is an object with data variables, it is a context.

filter(array, (value, key<optional>) -> {expression})

findIndex

Returns the first corresponding index of an array using a callback function.

Function Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value index will be returned, otherwise -1 will be returned.

Arguments:

  • array - the array to be modified
  • callback - a closure function with expression in the body which should be applied to every element in array

In this example datavars is an object with data variables, it is a context.

findIndex(array, (value, key<optional>) -> {expression})

foreach

Loop through an array of items and modify them by applying a new expression to each item

Arguments:

  • array - the array to be modified
  • callback - a closure function with expression in the body which should be applied to every element in array
  • keepKeys - If true - result array keys will be same as in original array, if false - result array keys will be numeric starting from 0. true by default.

In this example datavars is an object with data variables, it is a context.

tip

We can pass any expression as a body of a closure function. For instance, there can be 'foreach' inside 'foreach'

foreach(array, (value, key<optional>) -> {expression}, keepKeys = true)

includes

Checks if a value exists in an array

Arguments:

  • needle - the searched value
  • haystack - the array
includes(needle, haystack)

isset

Checks if the object or array has element or property with given key

Arguments:

  • array - array or object where key existence should be checked
  • key - the key to check existence
isset(array, key)

join

Join array elements with a string

join(array, separator)

joinKeys

Extract object keys as array elements and join them with a string

joinKeys(object, separator)

joinValues

Extract object values as array elements and join them with a string

joinValues(object, separator)

merge

Merge two or more objects together

merge(object, object, ...)

pluck

Returns a new list by plucking the same named property off all objects in the list supplied.

pluck(array, prop)

push

Push element onto the end of array

push(array, element)

rsort

Sort an array in descending order.

Arguments:

  • array - the array to be sorted
rsort(array)

serialize

To serialize array or object.

Arguments:

  • array - the array or object to be serialized
serialize(array)

set

Set the array element or object property value by offset.

Arguments:

  • array - the array or object to be modified
  • offset - offset string in dot notation or the bracket notation
  • value - the value to be set
set(array, offset, value)

sort

Sort an array in ascending order.

Arguments:

  • array - the array to be sorted
sort(array)

split

Split a string by a string

split(string, separator)

sum

Summarize elements values in an array

sum(array)

unserialize

To unserialize previously serialized array or object.

Arguments:

  • string - serialized array or object
unserialize(string)

unset

Unset the array element or object property by offset.

Arguments:

  • array - the array or object to be modified
  • offset - offset string in dot notation or the bracket notation
unset(array, offset)

unshift

Prepend element to the beginning of an array

unshift(array, element)

usort

Sort an array by values using a user-defined comparison function.

Arguments:

  • array - the array to be sorted
  • callback - the comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
usort(array, (a, b) -> {expression})