Skip to main content

Maps

Conversions and transforms for the native Map — to and from plain objects, merge, invert, map values. Map is the right structure for non-string keys, guaranteed insertion order and frequent adds and deletes, but it has none of the spread-and-merge ergonomics objects enjoy, which is the gap these fill. Every helper returns a new Map; later maps win on key collisions in mergeMaps, matching object spread.

Example

import { mapToObject, mergeMaps, objectToMap } from '@rtorcato/js-common/maps'

// Defaults plus overrides, then back to a plain object for JSON.
const merged = mergeMaps(
objectToMap({ theme: 'light', lang: 'en' }),
objectToMap({ theme: 'dark' })
)

mapToObject(merged) // { theme: 'dark', lang: 'en' } — later maps win

Import

import { invertMap, mapToObject, mapValues } from '@rtorcato/js-common/maps'

Exports

NameSummary
invertMapInverts a Map (swaps keys and values).
mapToObjectConverts a Map to an object.
mapValuesMaps the values of a Map using a function.
mergeMapsMerges two or more maps.
objectToMapConverts an object to a Map.

See also

  • objects — pick, omit, deepMerge, deepClone
  • arrays — chunk, unique, groupBy and other array helpers
  • json — safe parse/stringify and JSON deep clone