The answer from Jared Smith is great. 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
. These are different, but related, functions. prop
looks like
// prop :: k -> {k: v} -> v
prop('c', {a: 1, b: 2, c: 3, d: 4}); //=> 3
(there is some added complexity regarding undefined
.)
props
on the other hand takes multiple values
// 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 :: (k -> Bool) -> {k: v} -> {k: v}
The first parameter to filter
is a function from a (string) key to a boolean; it works with Javascript's idea that everything is truth-y except for a few specific values. 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. 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. 'a'.length; ==> 1
, 'a'[0]; //=> 'a'
. Hence props
can treat single-character strings as though they were one-element lists of character strings. But it can be a bit bizarre, too: R.props('cabby', {a: 1, b: 2, c: 3, d: 4}); //=> [3, 1, 2, 2, undefined]
.