mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.1 KiB
35 lines
1.1 KiB
9 years ago
|
var createAggregator = require('./_createAggregator');
|
||
|
|
||
|
/**
|
||
|
* Creates an object composed of keys generated from the results of running
|
||
|
* each element of `collection` through `iteratee`. The corresponding value
|
||
|
* of each key is the last element responsible for generating the key. The
|
||
|
* iteratee is invoked with one argument: (value).
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @category Collection
|
||
|
* @param {Array|Object} collection The collection to iterate over.
|
||
|
* @param {Function|Object|string} [iteratee=_.identity] The iteratee to transform keys.
|
||
|
* @returns {Object} Returns the composed aggregate object.
|
||
|
* @example
|
||
|
*
|
||
|
* var array = [
|
||
|
* { 'dir': 'left', 'code': 97 },
|
||
|
* { 'dir': 'right', 'code': 100 }
|
||
|
* ];
|
||
|
*
|
||
|
* _.keyBy(array, function(o) {
|
||
|
* return String.fromCharCode(o.code);
|
||
|
* });
|
||
|
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
||
|
*
|
||
|
* _.keyBy(array, 'dir');
|
||
|
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
|
||
|
*/
|
||
|
var keyBy = createAggregator(function(result, value, key) {
|
||
|
result[key] = value;
|
||
|
});
|
||
|
|
||
|
module.exports = keyBy;
|