Converts an array of objects into a Map, 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 Map. If empty, an empty Map is returned.
Param: keyProp
The key property or function used to derive the keys for the Map.
- If a string key, each object's property with this key is used as the map key.
- If a function, it will be called with each item to compute the key.
Param: valueProps
Optional. The value property, array of properties, or function used to derive the values for the Map.
- 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 Map where:
- The keys are derived from keyProp.
- 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.
// Case 1: No valueProps (entire object as value) constmapById = arrayToMap(data, 'id'); // Map<number, { id: number, name: string, age: number }>
// Case 2: Single valueProp constmapWithAge = arrayToMap(data, 'id', 'age'); // Map<number, number>
// Case 3: Array of valueProps constmapWithNameAndAge = arrayToMap(data, 'id', ['name', 'age']); // Map<number, { name: string, age: number }>
// Case 4: Function as valueProps constmapWithCustomValue = arrayToMap(data, 'id', item=> ({ fullName:item.name, isAdult:item.age >= 18 })); // Map<number, { fullName: string, isAdult: boolean }>
// Case 5: Function as keyProp constmapByCustomKey = arrayToMap(data, item=>`${item.name}-${item.age}`); // Map<string, { id: number, name: string, age: number }>
Converts an array of objects into a
Map
, 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
Map
. If empty, an emptyMap
is returned.Param: keyProp
The key property or function used to derive the keys for the
Map
. - If a string key, each object's property with this key is used as the map key. - If a function, it will be called with each item to compute the key.Param: valueProps
Optional. The value property, array of properties, or function used to derive the values for the
Map
. - 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
Map
where: - The keys are derived fromkeyProp
. - 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.Example