GuobaGuoba Utils

mapValues

@guoba-ai/utils / object / mapValues

Function: mapValues()

function mapValues<T, U>(obj, fn): Record<keyof T extends string ? keyof T : string, U>;

Defined in: object.ts:277

Transform the values of an object using a mapping function.

Parameters

obj

T

The source object

fn

(value, key) => U

Function that receives each value and key, returns the new value

Returns

Record<keyof T extends string ? keyof T : string, U>

A new object with transformed values

Example

mapValues({ a: 1, b: 2 }, value => value * 10) // { a: 10, b: 20 }

mapValues({ x: 1, y: 2 }, (value, key) => `${key}=${value}`)
// { x: 'x=1', y: 'y=2' }

mapValues({}, value => value) // {}

On this page