objectify
@guoba-ai/utils / array / objectify
Function: objectify()
Call Signature
function objectify<T, K>(array, keyFn): Record<K, T>;Defined in: array.ts:372
Convert an array to an object by mapping each item to a key-value pair.
Parameters
array
T[]
The array to convert
keyFn
(item) => K
Function that returns the key for each item
Returns
Record<K, T>
An object built from the key-value pairs
Example
objectify([{ id: 1, name: 'Alice' }], v => v.id, v => v.name)
// { 1: 'Alice' }
objectify([{ id: 'a' }, { id: 'b' }], item => item.id)
// { a: { id: 'a' }, b: { id: 'b' } }
objectify([{ id: 'a', value: 1 }, { id: 'a', value: 2 }], item => item.id, item => item.value)
// { a: 2 }Warning
If multiple items produce the same key, the later item overwrites the earlier one.
Call Signature
function objectify<T, K, V>(
array,
keyFn,
valueFn): Record<K, V>;Defined in: array.ts:376
Convert an array to an object by mapping each item to a key-value pair.
Parameters
array
T[]
The array to convert
keyFn
(item) => K
Function that returns the key for each item
valueFn
(item) => V
Returns
Record<K, V>
An object built from the key-value pairs
Example
objectify([{ id: 1, name: 'Alice' }], v => v.id, v => v.name)
// { 1: 'Alice' }
objectify([{ id: 'a' }, { id: 'b' }], item => item.id)
// { a: { id: 'a' }, b: { id: 'b' } }
objectify([{ id: 'a', value: 1 }, { id: 'a', value: 2 }], item => item.id, item => item.value)
// { a: 2 }Warning
If multiple items produce the same key, the later item overwrites the earlier one.