VDL Plugin SDK

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 ParameterDescription
KThe type of keys in the Map.
VThe type of values in the Map.
R-

Parameters

ParameterTypeDescription
mapMap<K, V>The Map to transform.
getNewValue(value, key, object) => RA 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
// }