| 1 |
var baseAssignValue = require('./_baseAssignValue'), |
| 2 |
createAggregator = require('./_createAggregator'); |
| 3 |
|
| 4 |
/** |
| 5 |
* Creates an object composed of keys generated from the results of running |
| 6 |
* each element of `collection` thru `iteratee`. The corresponding value of |
| 7 |
* each key is the last element responsible for generating the key. The |
| 8 |
* iteratee is invoked with one argument: (value). |
| 9 |
* |
| 10 |
* @static |
| 11 |
* @memberOf _ |
| 12 |
* @since 4.0.0 |
| 13 |
* @category Collection |
| 14 |
* @param {Array|Object} collection The collection to iterate over. |
| 15 |
* @param {Function} [iteratee=_.identity] The iteratee to transform keys. |
| 16 |
* @returns {Object} Returns the composed aggregate object. |
| 17 |
* @example |
| 18 |
* |
| 19 |
* var array = [ |
| 20 |
* { 'dir': 'left', 'code': 97 }, |
| 21 |
* { 'dir': 'right', 'code': 100 } |
| 22 |
* ]; |
| 23 |
* |
| 24 |
* _.keyBy(array, function(o) { |
| 25 |
* return String.fromCharCode(o.code); |
| 26 |
* }); |
| 27 |
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } |
| 28 |
* |
| 29 |
* _.keyBy(array, 'dir'); |
| 30 |
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } |
| 31 |
*/ |
| 32 |
var keyBy = createAggregator(function(result, value, key) { |
| 33 |
baseAssignValue(result, key, value); |
| 34 |
}); |
| 35 |
|
| 36 |
module.exports = keyBy; |