[英]How to filter out specific keys from object using Ramda?
http://ramdajs.com/0.21.0/docs/#prop http://ramdajs.com/0.21.0/docs/#prop
var myObject = {a: 1, b: 2, c: 3, d: 4};
var newObject = R.filter(R.props('a'), myObject);
//var newObject = R.filter(R.equals(R.props('a')), myObject);
console.log('newObject', newObject);
Right now the code above is returning the entire object:现在上面的代码正在返回整个对象:
newObject {"a":1,"b":2,"c":3,"d":4}
What I would like to do is just return a new object with just the 'a'
key.我想要做的只是返回一个只有'a'
键的新对象。 Or a new object with the a
and b
keys.或者一个带有a
和b
键的新对象。
The answer from Jared Smith is great. Jared Smith 的回答很棒。 I just wanted to add a note on why your code did not work.我只是想添加一个关于为什么你的代码不起作用的注释。 You tried你试过了
R.filter(R.props('a'), {a: 1, b: 2, c: 3, d: 4});
First of all, you pointed to the documentation for prop
, but used props
.首先,您指向了prop
的文档,但使用了props
。 These are different, but related, functions.这些是不同但相关的功能。 prop
looks like prop
看起来像
// prop :: k -> {k: v} -> v
prop('c', {a: 1, b: 2, c: 3, d: 4}); //=> 3
(there is some added complexity regarding undefined
.) (关于undefined
有一些额外的复杂性。)
props
on the other hand takes multiple values另一方面, props
采用多个值
// props :: [k] -> {k: v} -> [v]
props(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> [1, 4]
But neither of these is going to be useful input to filter
, which for these purposes we can think of as但是这些都不是filter
有用输入,出于这些目的,我们可以将其视为
// filter :: (k -> Bool) -> {k: v} -> {k: v}
The first parameter to filter
is a function from a (string) key to a boolean; filter
的第一个参数是一个从(字符串)键到布尔值的函数; it works with Javascript's idea that everything is truth-y except for a few specific values.它适用于 Javascript 的想法,即除了一些特定值之外,一切都是真实的。 It will be called with each key in turn.它将依次使用每个键调用。 So for example, when deciding whether to include {c: 3}
, it calls props('a')('c')
, which for another slightly odd reason * , actually works, returning [3]
, which is treated as truth-y, and the filter function will include {c: 3}
in its output.因此,例如,在决定是否包含{c: 3}
,它调用props('a')('c')
,由于另一个稍微奇怪的原因* ,实际上有效,返回[3]
,这被视为真理-y,过滤器函数将在其输出中包含{c: 3}
。 So too every key will be included.因此,每个键都将包含在内。
* The reason props('a', obj)
works when it really should be props(['a'], obj)
is that in JS, strings are close enough to lists, having a length
property and indexed values. * props('a', obj)
在它真正应该是props(['a'], obj)
时起作用的原因是,在 JS 中,字符串足够接近列表,具有length
属性和索引值。 'a'.length; ==> 1
'a'.length; ==> 1
, 'a'[0]; //=> 'a'
'a'.length; ==> 1
, 'a'[0]; //=> 'a'
'a'[0]; //=> 'a'
. 'a'[0]; //=> 'a'
。 Hence props
can treat single-character strings as though they were one-element lists of character strings.因此props
可以将单字符串视为字符串的单元素列表。 But it can be a bit bizarre, too: R.props('cabby', {a: 1, b: 2, c: 3, d: 4}); //=> [3, 1, 2, 2, undefined]
但它也可能有点奇怪: R.props('cabby', {a: 1, b: 2, c: 3, d: 4}); //=> [3, 1, 2, 2, undefined]
R.props('cabby', {a: 1, b: 2, c: 3, d: 4}); //=> [3, 1, 2, 2, undefined]
. R.props('cabby', {a: 1, b: 2, c: 3, d: 4}); //=> [3, 1, 2, 2, undefined]
.
const getOnlyGoods = R.pickBy((_, key) => key.includes('good')); const onlyGoods = getOnlyGoods({ "very good": 90, "good": 80, "good enough": 60, "average": 50, "bad": 30, "😡": 10, }); console.log(onlyGoods);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.