groupBy source npm
_.groupBy(collection, [iteratee=_.identity])
Creates an object composed of keys generated from the results of running
each element of collection
through iteratee
. The corresponding value
of each key is an array of the elements responsible for generating the key.
The iteratee is invoked with one argument: (value).
Arguments
- collection (Array|Object)
The collection to iterate over.
- [iteratee=_.identity] (Function|Object|string)
The function invoked per element.
Returns (Object)
Returns the composed aggregate object.
Example
_.groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }
// using the `_.property` callback shorthand
_.groupBy(['one', 'two', 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }