mapKeys
Import from @varavel/vdl-plugin-sdk/utils/maps.
function mapKeys<K, V>(map, getNewKey): Map<K, V>;
Creates a new Map with the same values but with keys transformed by the provided function.
This function takes a Map and a function that generates a new key from each value-key pair. It returns a new Map where the keys are the result of applying the function to each entry, while the values remain the same.
Type Parameters
| Type Parameter | Description |
|---|---|
K | The type of keys in the Map. |
V | The type of values in the Map. |
Parameters
| Parameter | Type | Description |
|---|---|---|
map | Map<K, V> | The Map to transform. |
getNewKey | (value, key, object) => K | A function that generates a new key from a value-key pair. |
Returns
Map<K, V>
A new Map with transformed keys and the same values.
Example
const map = new Map([
['a', 1],
['b', 2],
['c', 3]
]);
const result = mapKeys(map, (value, key) => key.toUpperCase());
// result will be:
// Map(3) {
// 'A' => 1,
// 'B' => 2,
// 'C' => 3
// }