Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
32 lines (30 sloc)
1.06 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import baseAssignValue from './.internal/baseAssignValue.js' | |
import reduce from './reduce.js' | |
/** | |
* Creates an object composed of keys generated from the results of running | |
* each element of `collection` thru `iteratee`. The corresponding value of | |
* each key is the last element responsible for generating the key. The | |
* iteratee is invoked with one argument: (value). | |
* | |
* @since 4.0.0 | |
* @category Collection | |
* @param {Array|Object} collection The collection to iterate over. | |
* @param {Function} iteratee The iteratee to transform keys. | |
* @returns {Object} Returns the composed aggregate object. | |
* @see groupBy, partition | |
* @example | |
* | |
* const array = [ | |
* { 'dir': 'left', 'code': 97 }, | |
* { 'dir': 'right', 'code': 100 } | |
* ] | |
* | |
* keyBy(array, ({ code }) => String.fromCharCode(code)) | |
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } | |
*/ | |
function keyBy(collection, iteratee) { | |
return reduce(collection, (result, value, key) => ( | |
baseAssignValue(result, iteratee(value), value), result | |
), {}) | |
} | |
export default keyBy |