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.
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.
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:
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.
Let's start by querying all records from the `cars` database table:
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:
Now, let's dive into the lambda function responsible for filtering the cars based on the user's criteria:
Here's a step-by-step breakdown of what's happening:
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.
Finally, we return the `filteredCars` array to the frontend:
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.
I found it helpful
I need more support