mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Adding groupBy.
This commit is contained in:
@@ -250,6 +250,21 @@
|
||||
return a < b ? -1 : a > b ? 1 : 0;
|
||||
}), 'value');
|
||||
};
|
||||
|
||||
// Groups the object's values by a criterion produced by an iterator
|
||||
_.groupBy = function(obj, iterator) {
|
||||
var result = {};
|
||||
each(obj, function(value) {
|
||||
var key = iterator(value);
|
||||
if (result.hasOwnProperty(key)) {
|
||||
result[key].push(value);
|
||||
}
|
||||
else {
|
||||
result[key] = [value];
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// Use a comparator function to figure out at what index an object should
|
||||
// be inserted so as to maintain order. Uses binary search.
|
||||
|
||||
Reference in New Issue
Block a user