mapValues
Creates a new object with the same keys as obj, but with each value replaced by iteratee(value, key, obj). Only own enumerable string keys are visited. Useful for projecting an object through a pure transformation while preserving its shape.
Try it
Section titled “Try it”Import
Section titled “Import”import { mapValues } from "1o1-utils";import { mapValues } from "1o1-utils/map-values";Signature
Section titled “Signature”function mapValues<T extends Record<string, unknown>, R>({ obj, iteratee,}: MapValuesParams<T, R>): Record<keyof T, R>Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
obj | Record<string, unknown> | Yes | The source object |
iteratee | (value, key, obj) => R | Yes | Function returning the new value for each pair |
Returns
Section titled “Returns”Record<keyof T, R> — A new object with the same keys and transformed values (input is not mutated).
Examples
Section titled “Examples”mapValues({ obj: { a: 1, b: 2 }, iteratee: (value) => value * 10,});// => { a: 10, b: 20 }mapValues({ obj: { ana: { age: 30 }, bob: { age: 25 } }, iteratee: (user) => user.age,});// => { ana: 30, bob: 25 }Edge Cases
Section titled “Edge Cases”- Throws if
objis not a plain object. - Throws if
iterateeis not a function. - Inherited properties are ignored — only own enumerable keys are visited.
- Output type can differ from input value type (full generic support).
Also known as
Section titled “Also known as”map values, transform values, value mapper, project values
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use mapValues to extract a single field from each value of a record