Reputation: 15157
With this code:
var objects = [{'a': 1}, {'b': 2}];
var result1, result2;
result1 = _(objects)
.filter(_.partialRight(_.has, 'a'))
.value();
result2 = _(objects)
.filter(function(o){ return _.partialRight(_.has, 'a')(o);})
.value();
result1
will be an empty array and result2
will be [{'a': 1}]
.
Why?
ps
Plunker:
http://plnkr.co/edit/6nhLBtq2id0GSYXVjhcX?p=preview
Upvotes: 1
Views: 109
Reputation: 254926
This answer is an addition to the @elclanrs' one:
That's how you could do what you want without creating an anonymous function:
result1 = _(objects)
.filter(_.compose(_.partialRight(_.has, 'a'), _.identity))
.value();
Explanation:
The _.compose()
function accepts the number of function references and applies them one by another, passing the result of execution from previous function to the next. And the _.identity()
function returns the first passed argument.
So if we unwrap the _.compose(_.partialRight(_.has, 'a'), _.identity)
expression it will look like:
function (a, b, c) {
return _.partialRight(_.has, 'a')(_.identity(a, b, c));
}
Online demo: http://plnkr.co/edit/kU3MkrfSJol9c9JbGBqo?p=preview
References:
Upvotes: 0
Reputation: 94101
What's happening is that filter
takes more arguments, that are being implicitly applied in composition, but not in application:
// what it looks like
.filter(_.partialRight(_.has, 'a'))
// what's happening
.filter(function(o,i,xs){return _.partialRight(_.has,'a')(o,i,xs);})
See here http://plnkr.co/edit/Q2Gz5vllUufzRvUuhqDQ?p=preview
Upvotes: 2