1. 6
    Declaratively Map Predicates to Object Properties Using Ramda where
    2m 34s
⚠️ This lesson is retired and might contain outdated information.

Declaratively Map Predicates to Object Properties Using Ramda where

Andy Van Slaars
InstructorAndy Van Slaars
Share this video with your friends

Social Share Links

Send Tweet

Sometimes you need to filter an array of objects or perform other conditional logic based on a combination of factors. Ramda's where function gives you a concise way to declaratively map individual predicates to object properties, that when combined, cover the various facets of your conditions. In this lesson, we'll look at how this powerful function can be used for scenarios requiring a complex predicate function.

[00:01] Here I've included Ramda and I have a list of product objects. I also have a predicate defined that simply returns true for every item and I'm piping my product data through a filter and a call to pluck that's going to grab the name off of each product that makes it through the filter. I assign that to results and I log it in.

[00:16] If I run this in its current state, I'll get back the names of every product in my product array. Let's say I just want to return items there in the clothes category. I jump back into the code and I can just replace my predicate with a function that takes in a product and checks the product category against the value "clothes."

[00:42] If I run this again, I'll get back just the jeans, hoodie, and sneakers. Let's say I want to filter this further and I only want clothes where the stock level is under 50. I can do that by just updating this and I can say prod.doc less than 50. I can run that again and I'll get back just the hoodie and the sneakers.

[01:05] What if I also had to filter on price? I could add a third condition to this predicate, but this is going to get messy and out of hand really fast. That's going to make it more error-prone, harder to read, and harder to follow.

[01:16] This is where Ramda's where function comes in. I'm going to take this entire predicate function and get rid of it. I'm going to replace it with a call to r.where and where is going to take an object as its first argument. This object is going to map keys from our target object to predicate functions that are going to check that property individually.

[01:36] Let's start with category. I want to filter for the clothes category so I'm going to use r.equals and just check that v equals clothes. I want to filter based on stock so I'll add a stock property. I want the items where the stock is under 50 so I'm going to use r.lessthan.

[01:57] The arguments for lessthan come in in a slightly different order than we're used to. I'm going to put a placeholder because I want my incoming data for my property value to go here. My second argument is going to be the value that I'm checking against.

[02:10] If I save this and I run it, you'll see that I get back hoodie and sneakers, just like I did before. But now my predicate is way more declarative. Adding a third check becomes trivial and let's say I just want to check against price, but this time I want to check for price where it's less than 100. I run that and I only get back the hoodie.