1. 8
    Add and Remove Items in Arrays using Filter, Reject and Partition in Ramda
    2m 9s
⚠️ This lesson is retired and might contain outdated information.

Add and Remove Items in Arrays using Filter, Reject and Partition in Ramda

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

Social Share Links

Send Tweet
Published 8 years ago
Updated 2 years ago

We'll learn how to get a subset of an array by specifying items to include with filter, or items to exclude using reject. We'll also look at how to get the results from both filter and reject, neatly separated with partition.

[00:00] Here I have an array of objects representing pets, three dogs and two cats. I've imported the Ramda library. I'm going to use Ramda to filter the pets array and return just the dogs.

[00:11] I'll start by defining a predicate function, which I'll call "dogcheck." Dogcheck is going to be a function that's going to take in a pet. It's going to check "pet.type," see if it's equal to "dog."

[00:27] To get my result, I'm going to call "R.filter," passing it dogcheck and the pets array. So we can see what's happening, I'm going to log out result. I can run that, and we should get a list back with just the three dogs.

[00:50] Let's say instead of the dogs, I want to get back the cats. What I could do is come up here and duplicate this. I could create "catcheck" that checks the pet's type against cat, call that as my predicate in my filter. When I run that, I'll get back the two cats rather than three dogs.

[01:12] Because I'm using Ramda, there's another way I can handle this. Let's get rid of catcheck and set this back to dogcheck. Of course, if I run this, I'm going to get the dogs, because I'm filtering based on dogcheck. What I can do, here, is I can change this filter to "reject," and I can have it reject the dogs, which will give me the cats.

[01:41] Let's say I need both dogs and cats, but I want to separate them based on this predicate. There's a way to do that, too. I can come in here, and I can say "R.partition," pass it a predicate function in my collection.

[01:57] When I run it, you'll see I get a nested array, where the first element are the items that match my predicate, so the results I would get for "filter." The second array are the results I would get for "reject."

Mike
Mike
~ 7 years ago

Nicely done. What tools did you use to record this?

Andy Van Slaars
Andy Van Slaarsinstructor
~ 7 years ago

Mike, thanks for watching!

I'm using Atom with the Atom Material UI and syntax themes. The terminal is the term3 plugin for Atom, though I use iTerm for my terminal the majority of the time. I record the screen and do my editing with Screenflow and the audio is recorded with a supercardoid mic connected through a USB audio interface.

David
David
~ 6 years ago

How could I extend this to filter global search style 1 character at a time to filter a nested array? The outer array are the parent rows displayed and the nested array is the expanded rows displayed under each parent. Is it possible to filter such an array?

pablo
pablo
~ 5 years ago

I love the functional programming and this library in particular, don't misunderstand my comment.

Is there any extra benefit in using ramda's filter method instead of the original JavaScript? I ask this, to clarify concepts, I understand that within the benefits offered by the filter of ramda, is to iterate objects and take advantage of the currificacion or partial application. But in this case you don't use either of those two benefits. If that is the case, don't you think it would be worthwhile to comment on the detail of when it's convenient to use a functional approach with this library and when to use original API from the language?

If I'm wrong and the method has other benefits please let me know.

Robert Pearce
Robert Pearce
~ 4 years ago

@pablo: The benefit is that now you can compose a specific filter operation and reuse it over and over with different data and even do so in a point-free way. I wrote an article on Ramda Chops: Map, Filter & Reduce, and this should help explain in detail. But for now:

const dogCheck = pet => pet.type === 'dog'
const getName = prop('name')
const onlyDogs = filter(dogCheck)
const getDogsNames = compose(map(prop('name')), onlyDogs)

getDogsNames([ /* one set of data */ ]) // some results
getDogsNames([ /* another set of data */ ]) // some different results
Markdown supported.
Become a member to join the discussionEnroll Today