Function mapUpdate

  • Updates the value for the given key in the map using the provided function. If the key does not exist in the map, the function will be called with undefined as the value.

    Type Parameters

    • K

      The type of the keys in the map.

    • V

    Parameters

    • map: Map<K, V>

      The map to update.

    • key: K

      The key of the value to update.

    • fun: ((value, key) => V)

      The function to use to update the value. This function takes the current value (or undefined if the key does not exist) and the key as arguments, and should return the new value.

        • (value, key): V
        • Parameters

          • value: undefined | V
          • key: K

          Returns V

    Returns V

    • The new value for the key.

    Example

    const map = new Map<string, number>()
    map.set('a', 1)
    mapUpdate(map, 'a', (value, key) => value !== undefined ? value + 1 : 0)
    map.get('a')
    //=> 2

Generated using TypeDoc