Arrays
Arrays are also known as lists. Arrays may contain a single item or they may contain many items. It's important to know that Arrays behave differently than other data types and typically require you to iterate over them in order to transform data. These iterations are usually done in the form of Loops.
However, Xano can abstract some common data transformation of Arrays by removing the Loops and some additional logic in the form of these Array functions:
Add to End of Array - Add an element to the end of an array.
Add to Beginning of Array - Add an element to the beginning of an array.
Remove from End of Array - Remove an element from the end of an array.
Remove from Beginning of Array - Remove an element from the beginning of an array.
Merge - Merge an array with an existing array.
Find First Element - Find the first matched element in an array.
Find First Element Index - Find the index of the first matched element in an array.
Has Any Element - Find out if there are any matched elements in an array.
Has Every Element - Find out if every element matches the expression you define.
Find All Elements - Find all the matching elements in an array.
Get Element Count - Get a count of the number of matched elements in an array.
Examples
For each of these examples, we will create a variable called array_1
with the value of Array
Add to End of Array
Add an element to the end of an array and return the updated array. This is an easy way to add a new value to the end of an existing array.
Choose the existing array's return variable. Then put the value you wish to add to the end of the array. (This value can of course be dynamic).
The result is the updated array with new value, 9, added to the end of it:
Add to Beginning of Array
Add an element to the beginning of an array and return the updated array. This is an easy way to add a new value to the beginning of an existing array.
Choose the existing array's return variable. Then put the value you wish to add to the beginning of the array. (This value can of course be dynamic).
The result is the updated array with new value, 99, added to the beginning of it:
Remove from End of Array
This function will automatically remove the element from the end of the array and return the update array. If you want to return the removed element, you can add an optional return variable.
Select the variable of the existing array that you wish to remove the last element from.
When returning array_1, the result is the updated array with the element on the end removed.
When returning the return variable, item, the result is the removed element.
Remove from Beginning of Array
This function will automatically remove the element from the beginning of the array and return the updated array. Add a return variable if you wish to return the removed element.
Select the variable of the existing array that you wish to remove the first element from.
The result is the updated array (array_1) with the element at the beginning removed.
The result of the return variable "example" will be the removed element.
Merge
Merge will merge one array with another existing array.
For this example, we will create a second array on the "value" line of the function. This is where we put the array that we wish to merge into the existing array.
We can create arrays by typing them into the input line or using push filters.
The result is the updated array_1 with the new array merged into it.
Merging is also accessible through filters such as merge and merge_recursive.
Note: The Expression Builder and $this Variable
The following array functions require the use of the Expression Builder. In these functions, the Expression Builder uses "where" statements to choose an element to look for.
The variable $this will be used as a variable substitution in the Expression Builder. $this represents the existing array you are searching through for each element of the array. $this is abstracting the complexity and need to loop through the array in order to transform or manipulate the data.
Find First Element
Find the first matched element.
This function requires the use of the Expression Builder to choose which element to look for.
In this example, we are looking for the first instance of the number 4. If there wasn't a number 4 then this would return as a "null" value.
The expression builder for this example looks like this.
The result is the first matched element from the array.
Find First Element Index
Finds the index of the first matched element. The index is the position of the element in an array starting at 0 as the first element.
This function requires the use of the Expression Builder to choose which element to look for.
In this example, we are looking for the index of the first instance of the number 3. If there wasn't a number 3 then this would return a -1.
The result:
In this example, array_1= [ 1, 2, 3, 4, 2, 1] and the first instance of 3 would have an index of 2
Has Any Element
Find out if there are any matched elements. This function will return a true or false value based on the result.
This function requires the use of the Expression Builder to choose which element to look for.
In this example, we are checking to see if the array has any values greater than(>) 2. If there is a number > 2 then this would return a true if not, it would return a false.
The result:
In the above example, we are checking if the array has any element that is greater than(>) 2, it returns a true.
Has Every Element
Find out if every element matches. This function will return true or false depending on the result. Each element of the array must match the expression for it to be true.
This function requires the use of the Expression Builder to choose which element to look for.
In this example, we are checking to see if all of the array values are greater than(>) 2. If all of the numbers are > 2 then this would return a true if not, it would return a false.
The result:
In the above example, we are checking if all of the elements are greater than(>) 2, it returns a false
Find All Elements
Find all the matched elements.
This function requires the use of the Expression Builder to choose which element to look for.
In this example, we are looking for elements that are greater than(>) 2. If any numbers are > 2 then this would return those values.
The result:
Get Element Count
Get the number of matched elements.
This function requires the use of the Expression Builder to choose which element to look for.
In this example, we are looking for the count of the elements that are = 2. If any numbers are = 2 then this would return the count of those elements.
The result:
In the example above, we are creating a new variable called find
and checking how many elements are = 2.
It returns 2 since array_1
has two values of 2.
Last updated