keyBy es-toolkit
Returns a new object with array elements converted to key-value pairs using a key-generating function.
Usage
keyBy(arr, getKeyFromItem)
Use keyBy when you want to create a key-indexed object for quick lookup of each element in an array. By providing a function that generates a unique key from each element, it creates an object where you can access elements using that key. If multiple elements generate the same key, the last element is used.
Complex key generation logic can also be used.
Parameters
The array to convert to an object.
A function that generates a key from each element, its index, and the array.
Returns
Returns an object with the generated keys as property names and the corresponding elements as values. If multiple elements generate the same key, the last element is used as the value.
Examples
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.