Array functions
count
Counts the number of elements in an array
- Description
- Example
- Result
count(array)
count(['Banana', 'Apple'])
2
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 modifiedcallback
- 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.
- Description
- Example
- Result
filter(array, (value, key<optional>) -> {expression})
datavars = {
"arrItem": [
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
}
filter(
arrItem,
(o) -> {
o.price > 1
}
)
[
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
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 modifiedcallback
- 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.
- Description
- Example
- Result
findIndex(array, (value, key<optional>) -> {expression})
datavars = {
"arrItem": [
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
}
findIndex(
arrItem,
(o) -> {
o.name == "carrot"
}
)
1
foreach
Loop through an array of items and modify them by applying a new expression to each item
Arguments:
array
- the array to be modifiedcallback
- a closure function with expression in the body which should be applied to every element in arraykeepKeys
- 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.
We can pass any expression as a body of a closure function. For instance, there can be 'foreach' inside 'foreach'
- Description
- Example
- Result
foreach(array, (value, key<optional>) -> {expression}, keepKeys = true)
datavars = {
"arrItem": [
{
"name": "apple",
"cat": "fruit",
"qty": 3,
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"qty": 5,
"price": 0.75
}
],
"catCurrencies": {
"fruit": "$",
"vegetable": "€"
}
}
foreach(
arrItem,
(o) -> {
merge(
o,
{"cost": catCurrencies[o.cat] ~ (o.price*o.qty)}
)
}
)
[
{
"name": "apple",
"cat": "fruit",
"qty": 3,
"price": 1.5,
"cost": "$4.5"
},
{
"name": "carrot",
"cat": "vegetable",
"qty": 5,
"price": 0.75,
"cost":"€3.75"
}
]
includes
Checks if a value exists in an array
Arguments:
needle
- the searched valuehaystack
- the array
- Description
- Example
- Result
includes(needle, haystack)
includes("name", ["name", "phone", "address])
true
isset
Checks if the object or array has element or property with given key
Arguments:
array
- array or object where key existence should be checkedkey
- the key to check existence
- Description
- Example
- Result
isset(array, key)
datavars = {
"arrItem": {
"name": "apple",
"cat": "fruit",
"price": 1.5
}
}
isset(arrItem, "name")
true
join
Join array elements with a string
- Description
- Example
- Result
join(array, separator)
join(['apple', 'orange', 'melon'], ', ')
join(['name', 'value'], ':')
apple, orange, melon
name:value
joinKeys
Extract object keys as array elements and join them with a string
- Description
- Example
- Result
joinKeys(object, separator)
joinKeys({ "apple": "$3.11", "orange": "$2.89", "melon": "$0.99" }, ', ')
apple, orange, melon
joinValues
Extract object values as array elements and join them with a string
- Description
- Example
- Result
joinValues(object, separator)
joinValues({ "apple": "$3.11", "orange": "$2.89", "melon": "$0.99" }, ', ')
$3.11, $2.89, $0.99
merge
Merge two or more objects together
- Description
- Example
- Result
merge(object, object, ...)
merge({"dog": "waf"}, {"cat": "purrr"})
merge({"dog": "waf"}, {"dog": "bow", "cat": "purrr"})
{"dog": "waf", "cat": "purrr"}
{"dog": "bow", "cat": "purrr"}
pluck
Returns a new list by plucking the same named property off all objects in the list supplied.
- Description
- Example
- Result
pluck(array, prop)
pluck([{name: 'fred', age: 29}, {name: 'wilma', age: 27}], 'name')
['fred','wilma']
push
Push element onto the end of array
- Description
- Example
- Result
push(array, element)
push(["orange", "banana"], "apple")
["orange", "banana", "apple"]
rsort
Sort an array in descending order.
Arguments:
array
- the array to be sorted
- Description
- Example
- Result
rsort(array)
rsort(["lemon", "orange", "banana", "apple"])
["orange", "lemon", "banana", "apple"]
serialize
To serialize array or object.
Arguments:
array
- the array or object to be serialized
- Description
- Example
- Result
serialize(array)
datavars = {
"arrItem": [
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
}
serialize(arrItem)
'[{"name":"apple","cat":"fruit","price":1.5},{"name":"carrot","cat":"vegetable","price":0.75},{"name":"cucumber","cat":"vegetable","price":1.3}]'
set
Set the array element or object property value by offset.
Arguments:
array
- the array or object to be modifiedoffset
- offset string in dot notation or the bracket notationvalue
- the value to be set
- Description
- Example
- Result
set(array, offset, value)
datavars = {
"arrItem": [
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
}
set(arrItem, "[0].price", 2.5)
[
{
"name": "apple",
"cat": "fruit",
"price": 2.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
sort
Sort an array in ascending order.
Arguments:
array
- the array to be sorted
- Description
- Example
- Result
sort(array)
sort(["lemon", "orange", "banana", "apple"])
["apple", "banana", "lemon", "orange"]
split
Split a string by a string
- Description
- Example
- Result
split(string, separator)
split('apple, orange, melon', ', ')
split('name:value', ':')
['apple', 'orange', 'melon']
['name', 'value']
sum
Summarize elements values in an array
- Description
- Example
- Result
sum(array)
sum([10, 20, 30])
60
unserialize
To unserialize previously serialized array or object.
Arguments:
string
- serialized array or object
- Description
- Example
- Result
unserialize(string)
datavars = {
"serializedArr": '[{"name":"apple","cat":"fruit","price":1.5},{"name":"carrot","cat":"vegetable","price":0.75},{"name":"cucumber","cat":"vegetable","price":1.3}]'
}
unserialize(serializedArr)
[
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
unset
Unset the array element or object property by offset.
Arguments:
array
- the array or object to be modifiedoffset
- offset string in dot notation or the bracket notation
- Description
- Example
- Result
unset(array, offset)
datavars = {
"arrItem": [
{
"name": "apple",
"cat": "fruit",
"price": 1.5
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
}
unset(arrItem, "[0].price")
[
{
"name": "apple",
"cat": "fruit"
},
{
"name": "carrot",
"cat": "vegetable",
"price": 0.75
},
{
"name": "cucumber",
"cat": "vegetable",
"price": 1.3
}
]
unshift
Prepend element to the beginning of an array
- Description
- Example
- Result
unshift(array, element)
unshift(["orange", "banana"], "apple")
["apple", "orange", "banana"]
usort
Sort an array by values using a user-defined comparison function.
Arguments:
array
- the array to be sortedcallback
- 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.
- Description
- Example
- Result
usort(array, (a, b) -> {expression})
usort([{"id": 4}, {"id": 8}, {"id": 2}], (a, b) -> {a.id - b.id})
[
{"id": 2},
{"id": 4},
{"id": 8}
]