Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply logical NOT to propEq and filter in ramda

I'm feeling my way though functional programming with Ramda and I'm struggling on something sounds like it should be easy.

I want all the entries in an array where a property does not equal a value.

For example in pure js

const filtered = source.filter(entry => entry.name !== 'something');

In Ramda there is a propEq so I can easily get all the elements that do match a value such as

const filtered = R.filter(R.propEq('name','something'),source);

But I can't see how to do the inverse of that, to only return values that do not equal a value.

I'm sure this must be very easy but I'm struggling to see a built in function to do it, the closest I can come up with is:

const others = R.filter(rate => rate.name !== name,res.rates);

But I'm sure there must be a better way?

like image 543
antfx Avatar asked Mar 10 '16 16:03

antfx


People also ask

What is Ramda in JavaScript?

Ramda is a JavaScript library that makes functional programming simple. While JavaScript libraries with functional capabilities have been around for a while, Ramda acts as a complete standard library specifically catered to the functional paradigm. Ramda is a JavaScript library that makes functional programming simple.

What is compose in ramda?

compose FunctionPerforms right-to-left function composition. The rightmost function may have any arity; the remaining functions must be unary. See also pipe.

Why should I use ramda?

What makes Ramda great is that their functions are automatically curried, which allows us to quickly build up sequences of small and straightforward functions or old functions that we already have created.

Is ramda better than Lodash?

Ramda is generally a better approach for functional programming as it was designed for this and has a community established in this sense. Lodash is generally better otherwise when needing specific functions (esp. debounce ).


1 Answers

Yes, Ramda has a reverse of filter called reject:

R.reject(R.propEq('name', 'something'))(source)

You can see this on the Ramda REPL

like image 119
Scott Sauyet Avatar answered Oct 20 '22 15:10

Scott Sauyet