Filtering Records Using a Lambda Function
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:
{
"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:
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:
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:
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:
- We create a function that takes two parameters:
cars(the array of all car records) anduserTags(the array of value arrays representing the user's filter criteria). - We iterate over each car in the
carsarray using thefiltermethod. - For each car, we check if it matches all the feature groups (value arrays) provided by the user.
- To check if a car matches a feature group, we use the
everymethod on theuserTagsarray. - Inside the
everycallback, we check if at least one value from the current feature group is present in the car'stagsarray using thesomemethod. - If a car matches all feature groups (i.e., it has at least one value from each group in its
tagsarray), it is included in thefilteredCarsarray.
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:
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.
Sign up for Xano.
100,000+ people build on Xano.
Start today and scale to millions.