Function fromPairs

  • Converts an array of key-value pairs into an object.

    Parameters

    • pairs: readonly any[]

      An array of key-value pairs where each key is a PropertyKey and each value is of type U.

    Returns Record<string, any>

    • An object where the keys are from the first element and values are from the second element.
    const pairs = [['a', 1], ['b', 2]];
    const result = fromPairs(pairs);
    // result will be: { a: 1, b: 2 }
  • Converts an array of key-value pairs into an object.

    Type Parameters

    • T extends PropertyKey

      The type of the keys in the resulting object. It must extend PropertyKey.

    • U

      The type of the values in the resulting object.

    Parameters

    • pairs: readonly [T, U][] | Map<T, U>

      An array of key-value pairs where each key is a PropertyKey and each value is of type U.

    Returns Record<T, U>

    • An object where the keys are of type T and the values are of type U.
    const pairs = [['a', 1], ['b', 2]];
    const result = fromPairs(pairs);
    // result will be: { a: 1, b: 2 }