Skip to main content

keyBy

_.keyBy(iteratee, collection) # Ⓢ [Ⓣ][1] Ⓝ

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

Arguments​

  1. iteratee (Function): The iteratee to transform keys.
  2. collection (Array|Object): The collection to iterate over.

Returns​

(Object): Returns the composed aggregate object.

Example​

var array = [
{ 'dir': 'left', 'code': 97 },
{ 'dir': 'right', 'code': 100 }
];

_.keyBy(function(o) {
return String.fromCharCode(o.code);
}, array);
// => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }

_.keyBy('dir', array);
// => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }