function mapValues
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.
Examples
Example 1
Example 1
import { mapValues } from "@std/collections/map_values"; import { assertEquals } from "@std/assert/assert_equals"; const usersById = { "a5ec": { name: "Mischa" }, "de4f": { name: "Kim" }, }; const namesById = mapValues(usersById, (it) => it.name); assertEquals( namesById, { "a5ec": "Mischa", "de4f": "Kim", }, );
Type Parameters
Parameters
Return Type
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.
Examples
Example 1
Example 1
import { mapValues } from "@std/collections/map_values"; import { assertEquals } from "@std/assert/assert_equals"; const usersById = { "a5ec": { name: "Mischa" }, "de4f": { name: "Kim" }, }; const namesById = mapValues(usersById, (it) => it.name); assertEquals( namesById, { "a5ec": "Mischa", "de4f": "Kim", }, );