Class DefaultMap<K, V, D>Category

A Map that has a default value for missing keys.

Example

const map = new DefaultMap(0);

map.get('a'); // 0

Example

const map = new DefaultMap(key => key.toUpperCase());

map.get('a'); // 'A'

Map

Type Parameters

  • K
  • V
  • D extends V = V

Hierarchy (view full)

Constructors

  • Create a new DefaultMap with a specified default value.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • K
    • V
    • D = V

    Parameters

    • defaultValue: Exclude<D, AnyFunction>

      The default value to use for missing keys

    • Optional entries: null | Iterable<readonly [unknown, unknown]>

    Returns DefaultMap<K, V, D>

    Example

    const map = new DefaultMap(0);
    
  • Create a new DefaultMap with a specified function to generate default values.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • K
    • V
    • D = V

    Parameters

    • defaultValueFn: ((key) => D)

      The function to generate default values

        • (key): D
        • Parameters

          • key: K

          Returns D

    • Optional entries: null | Iterable<readonly [unknown, unknown]>

    Returns DefaultMap<K, V, D>

    Example

    const map = new DefaultMap(key => key.toUpperCase());
    

Properties

[toStringTag]: string
defaultValueIsFunction: boolean

If the default value is a function.

defaultValueOrDefaultValueFn: Exclude<D, AnyFunction> | ((key) => D)

Type declaration

    • (key): D
    • Parameters

      • key: K

      Returns D

size: number

Returns

the number of elements in the Map.

[species]: MapConstructor

Methods

  • Returns an iterable of entries in the map.

    Returns IterableIterator<[K, V]>

  • Returns void

  • Parameters

    • key: K

    Returns boolean

    true if an element in the Map existed and has been removed, or false if the element does not exist.

  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[K, V]>

  • Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    • callbackfn: ((value, key, map) => void)
        • (value, key, map): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • Optional thisArg: any

    Returns void

  • Get the value for a given key, or the default value if it's missing.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • key: K

      The key to get the value for

    Returns V | D

    The value for key, or the default value if it's missing

    Example

    map.get('a');
    
  • Parameters

    • key: K

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Returns an iterable of keys in the map

    Returns IterableIterator<K>

  • Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

    Parameters

    • key: K
    • value: V

    Returns this

  • Returns an iterable of values in the map

    Returns IterableIterator<V>