⚠️ This lesson is retired and might contain outdated information.

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

InstructorAndy Van Slaars

Published 7 years ago
Updated 2 years ago

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.

[00:00] 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.

[00:18] 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.

[00:39] 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.

[01:03] 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.

[01:26] 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.

[01:46] 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.

[02:05] 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.

[02:14] 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.

~ 24 minutes ago

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Avoid meta-discussion

• This was great!
• This was horrible!
• I didn't like this because it didn't match my skill level.
• +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today