Skip to content

mapValues() function

Returns a new object with the same keys as obj, where each value is the result of applying f to the original value (and its key).

Signature:

typescript
declare function mapValues<T extends object, B>(f: (value: T[keyof T], key: keyof T) => Promise<B>, obj: T): Promise<{
    [K in keyof T]: B;
}>;

Example

ts
mapValues((v) => v * 2, { a: 1, b: 2 }); // { a: 2, b: 4 }
mapValues((v, k) => `${k}:${v}`, { a: 1 }); // { a: "a:1" }

// asynchronous callback
await mapValues(async (v) => v * 2, { a: 1, b: 2 }); // { a: 2, b: 4 }

// with pipe
pipe(
  { a: 1, b: 2 },
  mapValues((v) => v * 2),
); // { a: 2, b: 4 }

Open Source Code

Released under the Apache-2.0 License.