Reputation: 13800
When using the uniq()
method in underscore.js, we have the option of either a functional approach, or an OO aproach. Normally, uniq() takes an array, an isSorted
boolean, and an iterator
function. The boolean is for signalling whether or not the array has already been sorted. You can sort the array, then pass in true
for better performance (apparently).
It might look something like this:
var data = [
{'make':'Porsche','model':'911'},
{'make':'Porsche','model':'986'},
{'make':'Porsche','model':'986'}
];
var results = _.uniq(data, true, function (obj) {return obj.model});
For the chained version to work however, I’d have to do something like this:
var results = _.chain(data)
.uniq(function (obj) {return obj.model})
.value();
So, in the chained version, where does the isSorted
argument go?
Upvotes: 0
Views: 79
Reputation: 664484
The chained version will apply the same function only complemented by the wrapped value, so it takes the same arguments. The equivalent to
_.uniq(data, true, function (obj) {return obj.model});
is
_(data).uniq(true, function (obj) {return obj.model});
and your invocation without true
(the isSorted
argument is optional, you can put the iterator
mapper as second parameter as well) would be equivalent to
_.uniq(data, function (obj) {return obj.model});
Upvotes: 2