mapValues
import { mapValues } from "jsr:@ende/collections";function
function mapValues<T, O, K extends string>(record: Readonly<Record<K, T>>, transformer: (value: T, key: K) => O): Record<K, O>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.
record: Readonly<Record<K, T>>The record to map values from.
transformer: (value: T, key: K) => OThe function to transform each value.
Returns
A new record with all values transformed by the given transformer.
Example: Basic usage
import { mapValues } from "@ende/collections/map-values";
import { assertEquals } from "@ende/assert";
const usersById = {
a5ec: { name: "Mischa" },
de4f: { name: "Kim" },
};
const namesById = mapValues(usersById, (user) => user.name);
assertEquals(
namesById,
{
a5ec: "Mischa",
de4f: "Kim",
},
);
function
function mapValues<T, O, K extends string>(record: Readonly<Partial<Record<K, T>>>, transformer: (value: T, key: K) => O): Partial<Record<K, O>>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.
record: Readonly<Partial<Record<K, T>>>The record to map values from.
transformer: (value: T, key: K) => OThe function to transform each value.
Returns
A new record with all values transformed by the given transformer.
Example: Basic usage
import { mapValues } from "@ende/collections/map-values";
import { assertEquals } from "@ende/assert";
const usersById = {
"a5ec": { name: "Mischa" },
"de4f": { name: "Kim" },
};
const namesById = mapValues(usersById, (user) => user.name);
assertEquals(
namesById,
{
"a5ec": "Mischa",
"de4f": "Kim",
},
);
function
function mapValues<T, O, K extends string>(record: Record<K, T>, transformer: (value: T, key: K) => O): any