A utility function that converts an array to a keyed object. Pass a getter function to define which attribute becomes the key — handy when you need O(1) lookups instead of repeatedly scanning an array.
/**
* Converts an array to a keyed object using a custom attribute getter function.
* @param array The array to convert.
* @param attributeGetter The function to get the key from the item.
*/
function arrayToKeyedObject<T, K extends string>(
array: T[],
attributeGetter: (item: T) => K,
) {
if (!Array.isArray(array)) {
throw new TypeError('First argument must be an array')
}
if (!attributeGetter || typeof attributeGetter !== 'function') {
throw new TypeError('attributeGetter argument must be a function')
}
if (array.length === 0) {
return {} as Record<K, T>
}
const map = {} as Record<K, T>
for (const value of array) {
const key = attributeGetter(value)
map[key] = value
}
return map
}Usage example
Converting a users array to an object keyed by id:
interface User {
id: number
name: string
}
const users: User[] = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
]
const usersById = arrayToKeyedObject(users, (user) => String(user.id))
console.log(usersById)
// Output: { '1': { id: 1, name: 'Alice' }, '2': { id: 2, name: 'Bob' }, '3': { id: 3, name: 'Charlie' } }
const getUserById = (id: string) => usersById[id]
console.log(getUserById('2'))
// Output: { id: 2, name: 'Bob' }