The lodash method keyBy exported as a module for creating objects keyed by element properties.
npx @tessl/cli install tessl/npm-lodash--keyby@4.6.0The lodash method keyBy exported as a standalone module. Creates an object composed of keys generated from the results of running each element of a collection through an iteratee function. The corresponding value of each key is the last element responsible for generating the key.
npm install lodash.keybyvar keyBy = require('lodash.keyby');For ES modules (if supported by bundler):
import keyBy from 'lodash.keyby';var keyBy = require('lodash.keyby');
var array = [
{ 'dir': 'left', 'code': 97 },
{ 'dir': 'right', 'code': 100 }
];
// Using function iteratee
keyBy(array, function(o) {
return String.fromCharCode(o.code);
});
// => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
// Using property string iteratee
keyBy(array, 'dir');
// => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }Creates an object where keys are generated from running each element through an iteratee function and values are the elements themselves. When multiple elements generate the same key, the last element wins.
/**
* 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.
*
* @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.
*/
function keyBy(collection, iteratee);Parameters:
collection (Array|Object): The collection to iterate over. Can be an array of items or an object with enumerable properties.iteratee (Function|Object|string, optional): The iteratee to transform keys. Defaults to identity function if not provided.Returns:
Object: The composed aggregate object where keys are generated by the iteratee and values are the original elements.Iteratee Types:
Usage Examples:
Function Iteratee:
var users = [
{ 'name': 'barney', 'age': 36 },
{ 'name': 'fred', 'age': 40 }
];
keyBy(users, function(user) {
return user.name.charAt(0);
});
// => { 'b': { 'name': 'barney', 'age': 36 }, 'f': { 'name': 'fred', 'age': 40 } }String Property Iteratee:
keyBy(users, 'name');
// => { 'barney': { 'name': 'barney', 'age': 36 }, 'fred': { 'name': 'fred', 'age': 40 } }Numeric Index Iteratee (Arrays of Arrays):
var pairs = [[1, 'a'], [2, 'b'], [1, 'c']];
keyBy(pairs, 0);
// => { '1': [1, 'c'], '2': [2, 'b'] }
keyBy(pairs, 1);
// => { 'a': [1, 'a'], 'b': [2, 'b'], 'c': [1, 'c'] }Object Collections:
var scores = { 'math': 95, 'english': 88, 'science': 92 };
keyBy(scores, function(score) {
return score >= 90 ? 'excellent' : 'good';
});
// => { 'excellent': 92, 'good': 88 }Handling Object.prototype Properties:
// Safely handles built-in properties
keyBy([6.1, 4.2, 6.3], function(n) {
return Math.floor(n) > 4 ? 'hasOwnProperty' : 'constructor';
});
// => { 'hasOwnProperty': 6.3, 'constructor': 4.2 }Key Collision Behavior:
var data = [
{ id: 1, category: 'A' },
{ id: 2, category: 'B' },
{ id: 3, category: 'A' } // This will overwrite first element
];
keyBy(data, 'category');
// => { 'A': { id: 3, category: 'A' }, 'B': { id: 2, category: 'B' } }Edge Cases:
{}Compatibility:
@types/lodash.keyby