Skip to content

mapValues

Import from @varavel/vdl-plugin-sdk/utils/maps.

function mapValues<K, V, R>(map, getNewValue): Map<K, R>;

Creates a new Map with the same keys but with values transformed by the provided function.

This function takes a Map and a function that generates a new value from each value-key pair. It returns a new Map where the values are the result of applying the function to each entry, while the keys remain the same.

Type Parameters

Type Parameter Description
K The type of keys in the Map.
V The type of values in the Map.
R -

Parameters

Parameter Type Description
map Map\<K, V> The Map to transform.
getNewValue (value, key, object) => R A function that generates a new value from a value-key pair.

Returns

Map\<K, R>

A new Map with the same keys and transformed values.

Example

const map = new Map([
  ['a', 1],
  ['b', 2],
  ['c', 3]
]);
const result = mapValues(map, (value) => value * 2);
// result will be:
// Map(3) {
//   'a' => 2,
//   'b' => 4,
//   'c' => 6
// }