Auto-Incrementing ID for an Object List - Customer Corner
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.
Prerequisites
Before we begin, make sure you have the following:
- An existing table with a column containing an array of objects
- Each object in the array should have an
idproperty
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.
Step 1: Create a New API Endpoint
First, let's create a new API endpoint in Xano:
- Go to the "APIs" section and click "Create API".
- Give your API a descriptive name, such as "Add New Product Attribute".
- Add the following input fields:
product: The name of the product you want to updatecolor: The color of the new attributesize: The size of the new attribute
Step 2: Get the Existing Product Record
Next, we'll fetch the existing product record from the database:
- Add a new step and select the "Get Record" action.
- Set the
field nameto the column you're searching by (e.g.,name). - Set the
field valueto theproductinput.
This will retrieve the record containing the array of objects we want to modify.
Step 3: Increment the ID
To increment the ID, we'll need to:
- Get the last object in the array
- Retrieve its
idvalue - Increment the
idby 1 - Create a new object with the incremented
idand the providedcolorandsize
Here's how to do it in a single step:
- Add a new "Edit Record" step.
- Set the
field nameto the column containing the array of objects (e.g.,attributes). - Set the
valueto the following expression:
push(
{{products1.attributes}},
set(
set(
{},
"id",
get(pop({{products1.attributes}}), "id") + 1
),
"color",
{{inputs.color}}
),
"size",
{{inputs.size}}
)
This expression does the following:
pop({{products1.attributes}})gets the last object in theattributesarrayget(..., "id")retrieves theidvalue from that object... + 1increments theidby 1set({}, "id", ...)creates a new object with the incrementedidset(..., "color", {{inputs.color}})adds thecolorproperty from the inputset(..., "size", {{inputs.size}})adds thesizeproperty from the inputpush({{products1.attributes}}, ...)appends the new object to the existingattributesarray
- Return the edited record in the response.
Step 4: Test and Deploy
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.
Conclusion
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.
Sign up for Xano.
100,000+ people build on Xano.
Start today and scale to millions.