Converts an array of objects into a Record, using a specified key property or function,
and an optional value property, array of properties, or value transformation function.
Template: T
The type of objects in the array.
Template: K
The type of the key property or key function.
Template: V
The type of the value property, array of properties, or value function.
Param: array
The array of objects to convert into a Record. If empty, an empty Record is returned.
Param: keyProp
The key property or function used to derive the keys for the Record.
- If a string key, each object's property with this key is used as the record key.
- If a function, it will be called with each item to compute the key.
- The resulting keys will be converted to strings.
Param: valueProps
Optional. The value property, array of properties, or function used to derive the values for the Record.
- If undefined, the entire object is used as the value.
- If a single key, the corresponding property value is used.
- If an array of keys, an object with only those properties is used.
- If a function, it will be called with each item to compute the value.
Returns
A Record where:
- The keys are derived from keyProp and converted to strings.
- The values are derived from valueProps (or the entire object if valueProps is undefined).
Throws
If array is not an array.
Throws
If keyProp is a string and some objects in array are missing that property.
Throws
If keyProp function returns a value that cannot be converted to a string.
Converts an array of objects into a
Record
, using a specified key property or function, and an optional value property, array of properties, or value transformation function.Template: T
The type of objects in the array.
Template: K
The type of the key property or key function.
Template: V
The type of the value property, array of properties, or value function.
Param: array
The array of objects to convert into a
Record
. If empty, an emptyRecord
is returned.Param: keyProp
The key property or function used to derive the keys for the
Record
. - If a string key, each object's property with this key is used as the record key. - If a function, it will be called with each item to compute the key. - The resulting keys will be converted to strings.Param: valueProps
Optional. The value property, array of properties, or function used to derive the values for the
Record
. - Ifundefined
, the entire object is used as the value. - If a single key, the corresponding property value is used. - If an array of keys, an object with only those properties is used. - If a function, it will be called with each item to compute the value.Returns
A
Record
where: - The keys are derived fromkeyProp
and converted to strings. - The values are derived fromvalueProps
(or the entire object ifvalueProps
isundefined
).Throws
If
array
is not an array.Throws
If
keyProp
is a string and some objects inarray
are missing that property.Throws
If
keyProp
function returns a value that cannot be converted to a string.Example