Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Filter an Array Based on Multiple Predicates with Ramda's allPass Function

    Andy Van SlaarsAndy Van Slaars

    In this lesson, we'll filter a list of objects based on multiple conditions and we'll use Ramda's allPass function to create a joint predicate from multiple, individual predicate functions.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Here I have an array of car objects each with a name, the number of doors, and the miles per gallon. I've also pulled in some utility functions from Ramda that I'm going to use to filter the list of cars. Right now I'm using Ramda's filter, but my predicate function always returns true so all the cars will always be returned.

    Let's start by creating a predicate function that will return cars with good mileage. I'm going to create a constant here, and I'll call it "good mileage." I'm going to set this to equal a reference to prop satisfies, which I pulled in from Ramda. Prop satisfies is going to take its own predicate, which we're going to use LTE, which is less than or equal.

    We're going to pass it in the number that should be less than or equal to the property value. The property that we want to compare this to is miles per gallon from the car object. Then we can update our filter to use good mileage, and we'll see that it's cut out the SUV. Let's say I also want a filter based on the number of doors, so I'm going to come up here.

    I'm going to create a new constant. I'm going to call it four doors. I'm going to set this to equal prop equals from Ramda. I want to compare the doors property to the number four. I only want to get back cars where the doors is equal to four, so I'm going to replace this. We'll see that our filters apply. We've lost the compact car, but the SUV is back.

    In order to get back cars that have four doors and good mileage I need to combine these predicates. I don't want to create one function that has multiple conditions in it. I'd really like to keep these two individual predicates and just combine them in a nice, declarative way. What I'm going to do is I'm going to declare a constant, and I'm going to call it the perfect cars filter.

    I'm going to set this to equal a reference to Ramda's allPass function, which is going to take an array of predicate functions. We'll give it good mileage, and we'll give it four doors. Down here in my filter I'm going to do perfect cars.

    We'll see that our array is updated to only include the four-door sedan with 30 miles per gallon and the hybrid with four doors with 37 miles per gallon.

    By using allPass we set up our filter to pass our object into each individual predicate and only return the objects that evaluate to true for each individual predicate.