1 var baseAssignValue = require('./_baseAssignValue'),
2 createAggregator = require('./_createAggregator');
4 /** Used for built-in method references. */
5 var objectProto = Object.prototype;
7 /** Used to check objects for own properties. */
8 var hasOwnProperty = objectProto.hasOwnProperty;
11 * Creates an object composed of keys generated from the results of running
12 * each element of `collection` thru `iteratee`. The order of grouped values
13 * is determined by the order they occur in `collection`. The corresponding
14 * value of each key is an array of elements responsible for generating the
15 * key. The iteratee is invoked with one argument: (value).
20 * @category Collection
21 * @param {Array|Object} collection The collection to iterate over.
22 * @param {Function} [iteratee=_.identity] The iteratee to transform keys.
23 * @returns {Object} Returns the composed aggregate object.
26 * _.groupBy([6.1, 4.2, 6.3], Math.floor);
27 * // => { '4': [4.2], '6': [6.1, 6.3] }
29 * // The `_.property` iteratee shorthand.
30 * _.groupBy(['one', 'two', 'three'], 'length');
31 * // => { '3': ['one', 'two'], '5': ['three'] }
33 var groupBy = createAggregator(function(result, value, key) {
34 if (hasOwnProperty.call(result, key)) {
35 result[key].push(value);
37 baseAssignValue(result, key, [value]);
41 module.exports = groupBy;