Arrays and list functions
- Topics:
- Personalization
CREATED FOR:
- Experienced
- Developer
Use these functions to make interaction with arrays, lists, and strings easier.
Count only null
The countOnlyNull
function is used to count the number of null values in a list.
Syntax
{%= countOnlyNull(array) %}
Example
{%= countOnlyNull([4,0,1,6,0,0]) %}
Returns 3.
Count With Null
The countWithNull
function is used to count all the elements of a list including null values.
Syntax
{%= countWithNull(array) %}
Example
{%= countOnlyNull([4,0,1,6,0,0]) %}
Returns 6.
Distinct
The distinct
function is used to get values from an array or list with duplicate values removed.
Syntax
{%= distinct(array) %}
Example
The following operation specifies people who have placed orders in more than one store.
{%= distinct(person.orders.storeId).count() > 1 %}
Distinct Count With Null
The distinctCountWithNull
function is used to count the number of different values in a list including the null values.
Syntax
{%= distinctCountWithNull(array) %}
Example
{%= distinctCountWithNull([10,2,10,null]) %}
Returns 3.
First item
The head
function is used to return the first item in an array or list.
Syntax
{%= head(array) %}
Example
The following operation returns the first of the top five orders with the highest price. More information about the topN
function can be found in the first n
in array section.
{%= head(topN(orders,price, 5)) %}
First n
in array
The topN
function is used to return the first N
items in an array, when sorted in ascending order based on the given numerical expression.
Syntax
{%= topN(array, value, amount) %}
{ARRAY}
{VALUE}
{AMOUNT}
Example
The following operation returns the first five orders with the lowest price.
{%= topN(orders,price, 5) %}
In
The in
function is used to determine if an item is a member of an array or list.
Syntax
{%= in(value, array) %}
Example
The following operation defines people with birthdays in March, June, or September.
{%= in (person.birthMonth, [3, 6, 9]) %}
Includes
The includes
function is used to determine if an array or list contains a given item.
Syntax
{%= includes(array,item) %}
Example
The following operation defines people whose favorite color includes red.
{%= includes(person.favoriteColors,"red") %}
Intersects
The intersects
function is used to determine if two arrays or lists have at least one common member.
Syntax
{%= intersects(array1, array2) %}
Example
The following operation defines people whose favorite colors include at least one of red, blue, or green.
{%= intersects(person.favoriteColors,["red", "blue", "green"]) %}
Last n
in array
The bottomN
function is used to return the last N
items in an array, when sorted in ascending order based on the given numerical expression.
Syntax
{%= bottomN(array, value, amount) %}
{ARRAY}
{VALUE}
{AMOUNT}
Example
The following operation returns the last five orders with the highest price.
{%= bottomN(orders,price, 5) %}
Not in
The notIn
function is used to determine if an item is not a member of an array or list.
notIn
function also ensures that neither value is equal to null. Therefore, the results are not an exact negation of the in
function.Syntax
{%= notIn(value, array) %}
Example
The following operation defines people with birthdays that are not in March, June, or September.
{%= notIn(person.birthMonth ,[3, 6, 9]) %}
Subset of
The subsetOf
function is used to determine if a specific array (array A) is a subset of another array (array B). In other words, that all elements in array A are elements of array B.
Syntax
{%= subsetOf(array1, array2) %}
Example
The following operation defines people who have visited all of their favorite cities.
{%= subsetOf(person.favoriteCities,person.visitedCities) %}
Superset of
The supersetOf
function is used to determine if a specific array (array A) is a superset of another array (array B). In other words, that array A contains all elements in array B.
Syntax
{%= supersetOf(array1, array2) %}
Example
The following operation defines people who have eaten sushi and pizza at least once.
{%= supersetOf(person.eatenFoods,["sushi", "pizza"] %}