add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.keyBy

Overview

The lodash _.keyBy method is a function that creates an object composed of keys generated from the results of running each element of a collection through the specified function.

The function iterates over each element in the collection and generates a property name for that element by invoking the iteratee function. The value associated with each property name is the element that generated it.

This method is useful when working with collections of objects and you need to quickly access a specific object based on its property value. By using _.keyBy, you can create an object whose keys are the property values you want to search by, and the values associated with each key are arrays that contain all the objects in the collection that have that property value.

Here is an example of how to use _.keyBy:

const users = [
  { id: 1, name: 'John Doe', age: 30 },
  { id: 2, name: 'Jane Doe', age: 25 },
  { id: 3, name: 'Bob Smith', age: 35 }
]

const usersById = _.keyBy(users, 'id')
console.log(usersById)
// Output: { '1': { id: 1, name: 'John Doe', age: 30 }, '2': { id: 2, name: 'Jane Doe', age: 25 }, '3': { id: 3, name: 'Bob Smith', age: 35 } }

In this example, we pass in the users array as the first argument and 'id' as the second argument. This tells _.keyBy to generate property names based on the 'id' field of each user object. The resulting usersById object has keys that correspond to each user's ID, and the values are the user objects themselves.

Overall, _.keyBy is a powerful and versatile function that can save you a lot of time when working with collections of objects. It can be used in a variety of contexts, from filtering and sorting to data processing and visualization.