dynamic-forms / Function

keyBy

Generic types:T

Creates an object from an array, keyed by a specified property Native replacement for lodash keyBy()

Presentation

function keyBy(array: T[], key: keyof T): Record<string, T>;

Returns

Record<string, T> -

Object keyed by the specified property

Parameters

NameTypeDescription
array
T[]

Source array

key
keyof T

Property to use as key

Example usage

const users = [{ id: 'a', name: 'Alice' }, { id: 'b', name: 'Bob' }];
keyBy(users, 'id'); // { a: { id: 'a', name: 'Alice' }, b: { id: 'b', name: 'Bob' } }