1 var createAggregator = require('./_createAggregator');
3 /** Used for built-in method references. */
4 var objectProto = Object.prototype;
6 /** Used to check objects for own properties. */
7 var hasOwnProperty = objectProto.hasOwnProperty;
10 * Creates an object composed of keys generated from the results of running
11 * each element of `collection` through `iteratee`. The corresponding value
12 * of each key is an array of elements responsible for generating the key.
13 * The iteratee is invoked with one argument: (value).
17 * @category Collection
18 * @param {Array|Object} collection The collection to iterate over.
19 * @param {Function|Object|string} [iteratee=_.identity] The iteratee to transform keys.
20 * @returns {Object} Returns the composed aggregate object.
23 * _.groupBy([6.1, 4.2, 6.3], Math.floor);
24 * // => { '4': [4.2], '6': [6.1, 6.3] }
26 * // The `_.property` iteratee shorthand.
27 * _.groupBy(['one', 'two', 'three'], 'length');
28 * // => { '3': ['one', 'two'], '5': ['three'] }
30 var groupBy = createAggregator(function(result, value, key) {
31 if (hasOwnProperty.call(result, key)) {
32 result[key].push(value);
34 result[key] = [value];
38 module.exports = groupBy;