Search And Data Processing

Filtering Records Using a Lambda Function

Summary

In this guide, we'll explore how to use lambda functions to filter records in a database table where all attribute values are stored in a single array field. Follow along as we walk through an example of filtering cars based on body style, color, and year using a lambda function.

The Scenario: Filtering Cars on a Website

Imagine you have a car website where users can filter vehicles based on various attributes like make, body style, year, and more. In our example, the car records in the database are stored with all attributes saved in a single array field named `tags`.

Instead of having separate fields for color, year, body style, etc., the `tags` array contains all these values together for each car record.

Understanding the User Input

Before diving into the code, let's understand how the user input is structured. In our example, the user input is a JSON object with arrays representing different filter criteria.

Here's an example input:

json { "bodyStyle": ["SUV", "Sedan"], "color": ["Black", "Blue", "Silver"], "year": [2022, 2023] }

This input indicates that the user wants to find cars that are either SUVs or sedans, with colors black, blue, or silver, and from the years 2022 or 2023.

Setting Up the API Endpoint

Let's start by querying all records from the `cars` database table:

javascript const cars = await cno.query('cars');

Next, we'll convert the user input object into an array using the `getValuesFromObject` function. This step is necessary because we want to iterate over the values later:

javascript const userTags = getValuesFromObject(input);

The Lambda Function

Now, let's dive into the lambda function responsible for filtering the cars based on the user's criteria:

javascript const filteredCars = cars.filter((car) => { // Check if a car matches all feature groups (value arrays) return userTags.every((featureGroup) => { // Check if at least one value from the feature group is present in the car's tags return featureGroup.some((feature) => car.tags.includes(feature)); }); });

Here's a step-by-step breakdown of what's happening:

  1. We create a function that takes two parameters: `cars` (the array of all car records) and `userTags` (the array of value arrays representing the user's filter criteria).
  2. We iterate over each car in the `cars` array using the `filter` method.
  3. For each car, we check if it matches all the feature groups (value arrays) provided by the user.
  4. To check if a car matches a feature group, we use the `every` method on the `userTags` array.
  5. Inside the `every` callback, we check if at least one value from the current feature group is present in the car's `tags` array using the `some` method.
  6. If a car matches all feature groups (i.e., it has at least one value from each group in its `tags` array), it is included in the `filteredCars` array.

The lambda function returns an array of cars that match the user's filter criteria, ensuring that each car has at least one matching feature from every group of value arrays.

Returning the Filtered Results

Finally, we return the `filteredCars` array to the frontend:

javascript return filteredCars;

And that's it! By leveraging lambda functions, we can efficiently filter records stored in a non-traditional format, providing a seamless user experience for filtering cars on our website.

Remember, this is just one example of how lambda functions can be used for data manipulation. With Xano's no-code platform, you can explore various use cases and build powerful backend services without writing a single line of code.

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