Function mapEntries

  • Applies the given transformer to all entries in the given record and returns a new record containing the results.

    Type Parameters

    • T

      The type of the values in the input record.

    • O

      The type of the values in the output record.

    Parameters

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

      The record to map entries from.

    • transformer: ((entry: [string, T]) => [string, O])

      The function to transform each entry.

        • (entry): [string, O]
        • Parameters

          • entry: [string, T]

          Returns [string, O]

    Returns Record<string, O>

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

    import { mapEntries } from "@std/collections/map-entries";
    import { assertEquals } from "@std/assert";

    const usersById = {
    "a2e": { name: "Kim", age: 22 },
    "dfe": { name: "Anna", age: 31 },
    "34b": { name: "Tim", age: 58 },
    };

    const agesByNames = mapEntries(usersById, ([id, { name, age }]) => [name, age]);

    assertEquals(
    agesByNames,
    {
    Kim: 22,
    Anna: 31,
    Tim: 58,
    },
    );