简体   繁体   English

将逻辑NOT应用于propEq并在ramda中过滤

[英]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. 我感觉自己的方式虽然功能编程与Ramda和我正在努力的事情听起来应该很容易。

I want all the entries in an array where a property does not equal a value. 我想要一个属性等于值的数组中的所有条目。

For example in pure js 例如在纯粹的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 propEq有一个propEq所以我可以很容易地得到所有匹配值的元素

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? 但我相信一定有更好的方法吗?

Yes, Ramda has a reverse of filter called reject : 是的,Ramda有一个名为rejectfilter反向:

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

You can see this on the Ramda REPL 你可以在Ramda REPL上看到这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM