toMapByKey
Builds a Map from an iterable of items, keyed by a derived key. When two items derive the
same key, the later item wins (last-write-wins, matching Map.prototype.set semantics).
Named toMapByKey, not keyBy (lodash’s name for the same idea) — lodash’s _.keyBy
returns a plain object, not a Map; the to<Type> prefix (matching toSorted/toReversed)
makes the actual return type unambiguous.
Available since v3.0.3
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
items | Iterable<T> | The items to index |
fn | function | Function deriving the key for each item |
Returns
Section titled “Returns”Map<K, T> — A Map from derived key to item
Examples
Section titled “Examples”Index an array of records by id
Section titled “Index an array of records by id”Builds a Map keyed by a derived value, for O(1) lookup by that key.
Last item wins on collision
Section titled “Last item wins on collision”When two items derive the same key, the later one overwrites the earlier one.
