Skip to content

mapKeys

Creates a new Map with the same values but with each key transformed by a function. If two entries derive the same new key, the later one (in insertion order) wins.

Available since v3.0.3

import { mapKeys } from '@helpers4/map';
// or, from the all-in-one package (same code, one install):
import { mapKeys } from 'helpers4/map';
mapKeys<K, V, R>(map: ReadonlyMap<K, V>, fn: function): Map<R, V>
ParameterTypeDescription
mapReadonlyMap<K, V>The Map to transform
fnfunctionFunction invoked with (key, value) that returns the new key

Map<R, V> — A new Map with transformed keys

Creates a new Map with transformed keys and unchanged values.

mapKeys(new Map([['a', 1], ['b', 2]]), key => key.toUpperCase())
// => Map(2) { 'A' => 1, 'B' => 2 }

View source on GitHub