In this guide, we'll walk through how to automatically increment IDs when adding new objects to an existing array of objects using Xano's no-code platform. This can be useful when you need to maintain unique identifiers for elements within a collection, such as product attributes or order items.
Before we begin, make sure you have the following:
For this example, we'll use a `products` table with a column called `attributes` that stores an array of objects with `id`, `color`, and `size` properties.
First, let's create a new API endpoint in Xano:
Next, we'll fetch the existing product record from the database:
This will retrieve the record containing the array of objects we want to modify.
To increment the ID, we'll need to:
Here's how to do it in a single step:
push(
{{products1.attributes}},
set(
set(
{},
"id",
get(pop({{products1.attributes}}), "id") + 1
),
"color",
{{inputs.color}}
),
"size",
{{inputs.size}}
)
This expression does the following:
You can now test your API endpoint by providing a product name, color, and size. The new attribute should be added to the `attributes` array with an incremented `id`.
Once you're satisfied with the results, you can deploy your API and start using it in your applications.
Xano's no-code platform makes it easy to perform complex operations like incrementing IDs in an array of objects without writing any code. By following this guide, you should now be able to automatically generate unique identifiers when adding new elements to existing collections in your data models.
This transcript was AI generated to allow users to quickly answer technical questions about Xano.
I found it helpful
I need more support