mapEntries

import { mapEntries } from "jsr:@ende/collections";

function

function mapEntries<T, O>(record: Readonly<Record<string, T>>, transformer: (entry: [string, T]) => [string, O]): Record<string, O>

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

record: Readonly<Record<string, T>>

The record to map entries from.

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

The function to transform each entry.

Returns

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

Example: Basic usage

import { mapEntries } from "@ende/collections/map-entries";
import { assertEquals } from "@ende/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,
  },
);

Source