Function mapValues

  • Applies the given transformer to all values in the given record and returns a new record containing the resulting keys associated to the last value that produced them.

    Type Parameters

    • T

      The type of the values in the input record.

    • O

      The type of the values in the output record.

    • K extends string

      The type of the keys in the input and output records.

    Parameters

    • record: Readonly<Record<K, T>>

      The record to map values from.

    • transformer: ((value: T, key: K) => O)

      The function to transform each value.

        • (value, key): O
        • Parameters

          • value: T
          • key: K

          Returns O

    Returns Record<K, O>

    A new record with all values transformed by the given transformer.

    import { mapValues } from "@std/collections/map-values";
    import { assertEquals } from "@std/assert";

    const usersById = {
    a5ec: { name: "Mischa" },
    de4f: { name: "Kim" },
    };
    const namesById = mapValues(usersById, (user) => user.name);

    assertEquals(
    namesById,
    {
    a5ec: "Mischa",
    de4f: "Kim",
    },
    );
  • Applies the given transformer to all values in the given record and returns a new record containing the resulting keys associated to the last value that produced them.

    Type Parameters

    • T

      The type of the values in the input record.

    • O

      The type of the values in the output record.

    • K extends string

      The type of the keys in the input and output records.

    Parameters

    • record: Readonly<Partial<Record<K, T>>>

      The record to map values from.

    • transformer: ((value: T, key: K) => O)

      The function to transform each value.

        • (value, key): O
        • Parameters

          • value: T
          • key: K

          Returns O

    Returns Partial<Record<K, O>>

    A new record with all values transformed by the given transformer.

    import { mapValues } from "@std/collections/map-values";
    import { assertEquals } from "@std/assert";

    const usersById = {
    "a5ec": { name: "Mischa" },
    "de4f": { name: "Kim" },
    };
    const namesById = mapValues(usersById, (user) => user.name);

    assertEquals(
    namesById,
    {
    "a5ec": "Mischa",
    "de4f": "Kim",
    },
    );