Function mapGetOrElse

  • Returns a value from a map, while setting a given default value before returning it, if the key is not present.

    Type Parameters

    • K

      The type of the keys in the Map.

    • V

    Parameters

    • map: Map<K, V>

      map to get value from

    • key: K

      key to get value for

    • callback: ((key) => V)

      callback to set the value if key is not present

        • (key): V
        • Parameters

          • key: K

          Returns V

    Returns V

    The value retrieved from the Map or generated by the callback function.

    Example

    const map = new Map<string, number>()
    map.set('key', 1)
    mapGetOrElse(map, 'nonexistentKey', () => 2) // Output: 2

Generated using TypeDoc