4.17.11
3.10.1
2.4.2
1.3.1

_.keyBy(collection, [iteratee=_.identity])

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

argument
collection
[iteratee=_.identity]
type
(Array|Object)
Function
description
The collection to iterate over.
The iteratee to transform keys.

Returns

(array)

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 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 } }