Data Management

Object: Get Values

In Xano, working with objects is a common task, and sometimes you need to extract the values from an object's key-value pairs. This is where the handy getValues function comes into play. Let's dive in and learn how to use it effectively.

Understanding Objects

Before we explore getValues, let's quickly review the structure of objects. Objects in Xano are essentially key-value pairs. The keys (shown in yellow) represent the property names, and the values (shown on the right) are the corresponding data.

For example, let's look at a sample object from the products table:

{
 "id": 1,
 "name": "Coffee Mug",
 "quantity": 50
}

Here, id, name, and quantity are the keys, while 1, "Coffee Mug", and 50 are their respective values.

Using the getValues Function

The getValues function is designed to extract all the values from an object and return them as an array. Here's how you can use it:

  1. In the Function Stack, navigate to the "Data Manipulation" section and find the getValues function.
  2. The function expects an object as input. You can either pass an object directly or reference an existing object from your data.
  3. Assign the result to a variable for later use.

For example, let's extract the values from the products object we saw earlier:

const values = getValues(products_one);

After running this code, the values variable will contain an array with all the values from the products_one object, such as [1, "Coffee Mug", 50].

Extracting Values with Filters

Xano also provides an alternative way to extract values from objects using filters. You can achieve the same result as getValues by using the values filter.

Here's how you can use it:

const productValues = products.map(product => product.values());

This code will iterate over the products array and extract the values from each object using the values() filter.

Both the getValues function and the values filter are valid options for extracting values from objects in Xano. Choose the approach that best suits your coding style and project requirements.

Conclusion

The getValues function in Xano is a handy tool for extracting values from objects and converting them into an array. This can be useful in various scenarios, such as data manipulation, transformation, or preparation for further processing.

Remember, Xano provides multiple ways to achieve the same result, so feel free to explore and find the approach that works best for you. Happy coding!

Sign up for Xano

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free