Function objGetOrElse

  • This function attempts to retrieve a value from an object using a provided key. If the key does not exist in the object, it uses a callback function to generate a default value.

    Type Parameters

    • V

    Parameters

    • obj: Record<ObjectKey, V>

      The object from which to retrieve the value.

    • key: ObjectKey

      The key to use when retrieving the value.

    • callback: ((key) => V)

      The function to call when the key does not exist in the object. This function should return the default value.

        • (key): V
        • Parameters

          Returns V

    Returns V

    The value associated with the key in the object, or the result of the callback function if the key does not exist in the object.

    Example

    objGetOrElse({ a: 1, b: 2 }, 'c', (key) => 3);;
    //=> 3

    Typeparam

    V - The type of the values in the object.

Generated using TypeDoc