Data Management

Updating a single item within an object array

Summary

Are you struggling with updating a specific line item in a nested object list? Don't worry, we've got you covered! In this step-by-step guide, we'll show you how to easily update a nested object list in Xano, whether you're a seasoned developer or a no-code enthusiast.

The Scenario

Let's say you have a table called "stores" that contains an object array field called "hours". This field stores the opening and closing times for each day of the week. Your goal is to update the opening and closing times for a specific day within this nested object list.

Step 1: Get the Existing Data

First, you need to retrieve the existing data from the "stores" table. Here's how:

  1. Create a new API endpoint in Xano.
  2. Use the "Get Record" function to fetch the data from the "stores" table.
  3. Add an input field to specify the store record you want to retrieve.

GET_RECORD(stores, storeID)

Step 2: Define the New Data

Next, you'll need to define the new data you want to insert. Create an object called "newHours" with the following fields:

  • `day` (text field)
  • `open` (text field)
  • `close` (text field)

Step 3: Find the Index of the Line Item

To update a specific line item, you need to find its index within the "hours" array. Use the "Find First Element Index" function from the "Data Manipulation" category:

dayIndex = FIND_FIRST_ELEMENT_INDEX(stores.hours, this.day == newHours.day)

This function will return the index of the first element in the "hours" array where the `day` field matches the `day` field of the `newHours` object.

Step 4: Update the Variable

Now that you have the index, you can update the "hours" array with the new data. Use the "Update Variable" function and the "Set Filter" option:

UPDATE_VARIABLE(stores.hours, SET_FILTER(dayIndex, newHours))

This will replace the line item at the specified index with the `newHours` object.

Step 5: Update the Record

Finally, use the "Edit Record" function to update the record in the database with the modified "hours" array:

EDIT_RECORD(stores, storeID, hours=stores.hours)

And that's it! You've successfully updated a nested object list in Xano.

Example Usage

Here's an example of how you can use this functionality:

  1. Set the `storeID` input to `1`.
  2. Set the `newHours` object with the following values:
  • `day`: "Monday"
  • `open`: "09:00"
  • `close`: "17:00"

When you run the API endpoint, it will update the "hours" array for the store with ID `1`, changing the opening and closing times for Monday.

By following these steps, both non-technical users and developers can easily update nested object lists in Xano, saving time and effort in their application development process.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free