Filtering Data

FaasPlus allows you to filter data using JavaScript’s powerful filtering capabilities. You can apply JavaScript filter methods to refine data according to your requirements.


Example 1: Filter Cities by Population

In this example, we send an array of city objects (each containing a country and population) in the request body. The function filters the cities to return only those with a population greater than 22 million.


const handleRequest = async (req) => {
    const cities = req.body.cities || [];
    const crowdedCities = cities.filter(city => city.population > 22000000);
    return crowdedCities;
};
body (as json)
{
    "cities": [
        { "city": "Tokyo", "country": "Japan", "population": 37000000 },
        { "city": "Delhi", "country": "India", "population": 31000000 },
        { "city": "Shanghai", "country": "China", "population": 27000000 },
        { "city": "São Paulo", "country": "Brazil", "population": 22000000 },
        { "city": "Mumbai", "country": "India", "population": 20000000 },
        { "city": "Mexico City", "country": "Mexico", "population": 21000000 },
        { "city": "Cairo", "country": "Egypt", "population": 21000000 },
        { "city": "Beijing", "country": "China", "population": 20000000 },
        { "city": "Dhaka", "country": "Bangladesh", "population": 21000000 },
        { "city": "New York City", "country": "USA", "population": 19000000 }
    ]
}

>response
[
    { "city": "Tokyo", "country": "Japan", "population": 37000000 },
    { "city": "Delhi", "country": "India", "population": 31000000 },
    { "city": "Shanghai", "country": "China", "population": 27000000 }
]

Example 2: Filter Cities by Population using query string param

In this example, we use a query string parameter to set a custom population threshold. The function then filters the cities to return only those with a population greater than the specified value.


const handleRequest = async (req) => {
    const cities = req.body.cities || [];
    const pop = parseInt(req.params.pop) || 0;
    const crowdedCities = cities.filter(city => city.population > pop);
    return crowdedCities;
};
body (as json)
{
    "cities": [
        { "city": "Tokyo", "country": "Japan", "population": 37000000 },
        { "city": "Delhi", "country": "India", "population": 31000000 },
        { "city": "Shanghai", "country": "China", "population": 27000000 },
        { "city": "São Paulo", "country": "Brazil", "population": 22000000 },
        { "city": "Mumbai", "country": "India", "population": 20000000 },
        { "city": "Mexico City", "country": "Mexico", "population": 21000000 },
        { "city": "Cairo", "country": "Egypt", "population": 21000000 },
        { "city": "Beijing", "country": "China", "population": 20000000 },
        { "city": "Dhaka", "country": "Bangladesh", "population": 21000000 },
        { "city": "New York City", "country": "USA", "population": 19000000 }
    ]
}

>response
[
    { "city": "Tokyo", "country": "Japan", "population": 37000000 },
    { "city": "Delhi", "country": "India", "population": 31000000 },
]