Data Management

Update An Object Array

Summary

Hey there! Updating objects and object arrays can indeed be tricky, but don't worry, we'll break it down step-by-step so you can master this in no time. Let's dive right in!

Step 1: Retrieve the Record

Before we can update an object or object array, we need to retrieve the record we want to modify. We'll use the `get_record` function to fetch the record with the desired fields.

javascript let stats = get_record('users', { filter: { id: 'user_id' }, fields: ['game_day_option', 'three_point_field_goal', 'game_day_stats'] });

This will fetch the record with the specified `user_id` and include the `game_day_option`, `three_point_field_goal`, and `game_day_stats` fields.

Step 2: Update a Single Object

If you want to update a single object within an object array, you'll use the `append` function. Here's how it works:

javascript let updated = update_variable({ data: { game_day_stats: stats.game_day_stats.append({ rebounds: 12, turnovers: 1 }) }, keys: ['id'], records: [stats] });

In this example, we're creating a new variable `updated` that updates the `game_day_stats` field by appending a new object with `rebounds` and `turnovers` values.

The `update_variable` function takes three arguments:

  1. `data`: An object containing the updated fields and their new values
  2. `keys`: An array specifying the unique key(s) for the record
  3. `records`: An array containing the original record(s) to be updated

After running this code, the `updated` variable will contain the record with the updated `game_day_stats` field.

Step 3: Update an Object Array

If you need to update an entire object array, you'll use the `merge` function instead of `append`. Here's how:

javascript let updated = update_variable({ data: { game_day_stats: stats.game_day_stats.merge([ { assists: 10, points: 43 } ]) }, keys: ['id'], records: [stats] });

In this example, we're creating a new `updated` variable that merges the `game_day_stats` field with a new array containing an object with `assists` and `points` values.

The `merge` function replaces the entire object array with the new array provided, whereas `append` adds new objects to the existing array.

Step 4: Return the Updated Record

After updating the record, you can return it using the `return` function:

javascript return updated;

This will make the updated record available for further processing or storage.

And that's it! You now know how to update objects and object arrays in Xano. Remember, whenever you're dealing with an object array, use `merge` to replace the entire array or `append` to add new objects to the existing array.

Keep practicing, and you'll become a pro at updating objects and object arrays in no time!

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