Function toObject

  • Converts the source to a record.

    Type Parameters

    • T

      Type of items in the source.

    • TKey extends string | number | symbol

      Type of the key.

    • TValue

      Type of the value.

    Parameters

    • keySelector: ((entry: T) => TKey)

      Key selector function.

    • valueSelector: ((entry: T) => TValue)

      Value selector function.

    Returns CollectorFunction<T, Record<TKey, TValue>>

    Collector that converts the source to a record.

    const result = toRecord<number, string, number>((x) => x.toString(), (x) => x * 2)([1, 2, 3]);
    console.log(result);
    // {
    // '1': 2,
    // '2': 4,
    // '3': 6,
    // }