mapFields
接收一个 obj 对象和 mapper 函数,遍历对象每一个字段,返回一个以 mapper 返回值为 value 的新对象。
Added in v0.0.6
Usage
ts
import { mapFields } from 'parsnip-kit'
const input = { name: 'alice', city: 'tokyo' } as const
const result = mapFields(input, (v) => v.length)
// { name: 5, city: 5 }API
Type Parameter
| Arg | Type | Description |
|---|---|---|
T | extends object | 对象类型 |
V | | 迭代器函数的返回类型 |
Arguments
| Arg | Type | Optional | Default | Description |
|---|---|---|---|---|
obj | T | false | undefined | 要遍历的对象 |
mapper | <K extends keyof T>(value: T[K], key: K, object: T) => V | false | undefined | 迭代器函数 |
Returns
| Type |
|---|
Record<keyof T, V> |