Skip to main content

@rtorcato/js-common / maps

maps

Functions​

invertMap()​

invertMap<K, V>(map): Map<V, K>

Defined in: maps/index.ts:13

Inverts a Map (swaps keys and values). If duplicate values exist, later keys overwrite earlier ones.

Example​

invertMap(new Map([['a', 1], ['b', 2]]))
// Map { 1 => 'a', 2 => 'b' }

Type Parameters​

K​

K

V​

V

Parameters​

map​

Map<K, V>

The map to invert.

Returns​

Map<V, K>

The inverted map.


mapValues()​

mapValues<K, V, U>(map, fn): Map<K, U>

Defined in: maps/index.ts:34

Maps the values of a Map using a function.

Example​

mapValues(new Map([['a', 1], ['b', 2]]), (v) => v * 10)
// Map { 'a' => 10, 'b' => 20 }

Type Parameters​

K​

K

V​

V

U​

U

Parameters​

map​

Map<K, V>

The map to map over.

fn​

(value, key) => U

The function to apply to each value.

Returns​

Map<K, U>

A new map with mapped values.


mergeMaps()​

mergeMaps<K, V>(...maps): Map<K, V>

Defined in: maps/index.ts:54

Merges two or more maps. Later maps overwrite earlier keys.

Example​

mergeMaps(new Map([['a', 1]]), new Map([['a', 9], ['b', 2]]))
// Map { 'a' => 9, 'b' => 2 }

Type Parameters​

K​

K

V​

V

Parameters​

maps​

...Map<K, V>[]

The maps to merge.

Returns​

Map<K, V>

The merged map.


objectToMap()​

objectToMap<T>(obj): Map<string, any>

Defined in: maps/index.ts:70

Converts an object to a Map.

Type Parameters​

T​

T extends object

Parameters​

obj​

T

The object to convert.

Returns​

Map<string, any>

The resulting map.


mapToObject()​

mapToObject<V>(map): Record<string, V>

Defined in: maps/index.ts:79

Converts a Map to an object.

Type Parameters​

V​

V

Parameters​

map​

Map<string, V>

The map to convert.

Returns​

Record<string, V>

The resulting object.