Data Management

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:

  1. Create an input field to provide the ID of the record you want to update.
  2. Create a text input field to hold the new value you want to add.
  3. Use the get action to retrieve the existing record by its ID.
  4. Update the 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)
  1. Run the action, and you'll see the new value added to the array_texts column in your database.

Adding an Array of Values to an Array

To add an array of values to an existing array, follow these steps:

  1. Create an input field to hold the array of new values you want to add.
  2. Use the get action to retrieve the existing record by its ID.
  3. Update the 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)
  1. Run the action, and you'll see the new array of values appended to the existing array_texts column in your database.

Adding an Object to an Array of Objects

To add a new object to an array of objects, follow these steps:

  1. Create an object input field with the same schema as the array_objects column in your database.
  2. Use the get action to retrieve the existing record by its ID.
  3. Update the 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)
  1. Run the action, and you'll see the new object added to the array_objects column 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:

  1. Declare an empty array variable to store the transformed data.
  2. Use the forEach loop to iterate over the original array.
  3. Within the loop, perform the desired data transformations on each element.
  4. 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.

  1. Create a variable to hold the array you want to transform.
  2. Use the set filter to specify the field(s) you want to modify.
  3. 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

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free