How to Work with Arrays in Xano
Welcome to this tutorial on working with arrays in Xano, a no-code platform for building and deploying backend services. In this guide, we'll cover how to update records with arrays of items, query for specific values within arrays, and perform data transformations using forEach loops and the powerful Zetta Transform feature.
Updating Records with Array Values
Let's start by exploring how to update a record with an array of values in your database. Suppose you have a table schema with an array_texts column (an array of single values) and an array_objects column (an array of objects).
Adding a Single Value to an Array
To add a new value to an existing array, follow these steps:
- Create an input field to provide the ID of the record you want to update.
- Create a text input field to hold the new value you want to add.
- Use the
getaction to retrieve the existing record by its ID. - Update the
array_textsfield by referencing the existing values from thegetaction and applying thepushfilter. Thepushfilter appends the new value to the end of the array.
get_record.array_texts.push(new_value_input)
- Run the action, and you'll see the new value added to the
array_textscolumn in your database.
Adding an Array of Values to an Array
To add an array of values to an existing array, follow these steps:
- Create an input field to hold the array of new values you want to add.
- Use the
getaction to retrieve the existing record by its ID. - Update the
array_textsfield by referencing the existing values from thegetaction and applying themergefilter. Themergefilter combines the existing array with the new array of values.
get_record.array_texts.merge(new_array_input)
- Run the action, and you'll see the new array of values appended to the existing
array_textscolumn in your database.
Adding an Object to an Array of Objects
To add a new object to an array of objects, follow these steps:
- Create an object input field with the same schema as the
array_objectscolumn in your database. - Use the
getaction to retrieve the existing record by its ID. - Update the
array_objectsfield by referencing the existing values from thegetaction and applying thepushfilter. Thepushfilter appends the new object to the end of the array.
get_record.array_objects.push(new_object_input)
- Run the action, and you'll see the new object added to the
array_objectscolumn in your database.
Querying Specific Values in Arrays
Xano provides powerful querying capabilities to filter and retrieve specific values from arrays. Let's explore two methods: findFirstElement and findAllElements.
Finding the First Matching Element
The findFirstElement function searches through an array and returns the first element that matches the specified condition. The condition is defined using a custom query syntax similar to a database query.
array.findFirstElement($ => $.field == 'value')
In this example, $ represents each item in the array, and you can access its properties using dot notation (e.g., $.field). The condition checks if the field property of the item equals the specified 'value'.
Finding All Matching Elements
The findAllElements function works similarly to findFirstElement, but it returns an array containing all elements that match the specified condition.
array.findAllElements($ => $.field == 'value')
This function allows you to retrieve multiple matching items from the array.
Transforming Data in Arrays
Xano provides two methods for transforming data within arrays: forEach loops and the powerful Zetta Transform feature.
Using forEach Loops
The forEach loop iterates over each element in an array and allows you to perform transformations on the data. Here's an example:
- Declare an empty array variable to store the transformed data.
- Use the
forEachloop to iterate over the original array. - Within the loop, perform the desired data transformations on each element.
- Push the transformed element to the new array.
let transformedArray = [];
originalArray.forEach(item => {
let transformedItem = /* perform transformations */;
transformedArray.push(transformedItem);
});
This approach ensures that the original data remains untouched while creating a new array with the transformed values.
Using Zetta Transform
Zetta Transform is a powerful feature in Xano that allows you to perform data transformations more efficiently. It leverages a declarative syntax and optimized processing, making it faster and more readable than traditional loops.
- Create a variable to hold the array you want to transform.
- Use the
setfilter to specify the field(s) you want to modify. - Apply the desired transformation using the Zetta Transform syntax:
$$|transform_function.
transformed_array = original_array.set($.field, $$|transform_function)
The Zetta Transform syntax uses the $$ symbol to represent the current value of the field, and the | (pipe) character to apply the specified transformation function.
For more information on the available Zetta Transform functions and their syntax, refer to the official Xano documentation or check out the tutorial videos on YouTube.
With these techniques for working with arrays in Xano, you can efficiently update records, query specific values, and transform data within your applications. If you have any questions or need further assistance, don't hesitate to reach out to the Xano community or contact the live support team within the platform.
Happy coding!
Sign up for Xano.
100,000+ people build on Xano.
Start today and scale to millions.