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.
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).
To add a new value to an existing array, follow these steps:
get action to retrieve the existing record by its ID.array_texts field by referencing the existing values from the get action and applying the push filter. The push filter appends the new value to the end of the array.get_record.array_texts.push(new_value_input)
array_texts column in your database.To add an array of values to an existing array, follow these steps:
get action to retrieve the existing record by its ID.array_texts field by referencing the existing values from the get action and applying the merge filter. The merge filter combines the existing array with the new array of values.get_record.array_texts.merge(new_array_input)
array_texts column in your database.To add a new object to an array of objects, follow these steps:
array_objects column in your database.get action to retrieve the existing record by its ID.array_objects field by referencing the existing values from the get action and applying the push filter. The push filter appends the new object to the end of the array.get_record.array_objects.push(new_object_input)
array_objects column in your database.Xano provides powerful querying capabilities to filter and retrieve specific values from arrays. Let's explore two methods: findFirstElement and findAllElements.
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'.
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.
Xano provides two methods for transforming data within arrays: forEach loops and the powerful Zetta Transform feature.
The forEach loop iterates over each element in an array and allows you to perform transformations on the data. Here's an example:
forEach loop to iterate over the original 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.
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.
set filter to specify the field(s) you want to modify.$$|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!
Join 100,000+ people already building with Xano.
Start today and scale to millions.
Start building for free