Skip to content

mapValues() function

obj と同じキーを持ち、各値が元の値(とキー)に f を適用した結果である新しいオブジェクトを返します。非同期コールバックを使うと Promise を返します。

Signature:

typescript
function mapValues<T extends object, B>(
  f: (value: T[keyof T], key: keyof T) => B,
  obj: T,
): { [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" }

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

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

Open Source Code

Released under the Apache-2.0 License.