1 | var baseAssignValue = require('./_baseAssignValue'),
|
---|
2 | createAggregator = require('./_createAggregator');
|
---|
3 |
|
---|
4 | /** Used for built-in method references. */
|
---|
5 | var objectProto = Object.prototype;
|
---|
6 |
|
---|
7 | /** Used to check objects for own properties. */
|
---|
8 | var hasOwnProperty = objectProto.hasOwnProperty;
|
---|
9 |
|
---|
10 | /**
|
---|
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).
|
---|
16 | *
|
---|
17 | * @static
|
---|
18 | * @memberOf _
|
---|
19 | * @since 0.1.0
|
---|
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.
|
---|
24 | * @example
|
---|
25 | *
|
---|
26 | * _.groupBy([6.1, 4.2, 6.3], Math.floor);
|
---|
27 | * // => { '4': [4.2], '6': [6.1, 6.3] }
|
---|
28 | *
|
---|
29 | * // The `_.property` iteratee shorthand.
|
---|
30 | * _.groupBy(['one', 'two', 'three'], 'length');
|
---|
31 | * // => { '3': ['one', 'two'], '5': ['three'] }
|
---|
32 | */
|
---|
33 | var groupBy = createAggregator(function(result, value, key) {
|
---|
34 | if (hasOwnProperty.call(result, key)) {
|
---|
35 | result[key].push(value);
|
---|
36 | } else {
|
---|
37 | baseAssignValue(result, key, [value]);
|
---|
38 | }
|
---|
39 | });
|
---|
40 |
|
---|
41 | module.exports = groupBy;
|
---|