Skip to content

formatData

1.3.0

格式化输入的数据(对象或数组)

基本用法

根据提供的选项进行数据格式化,包括映射关系、是否保留原始键以及额外的数据

typescript
import { formatData } from 'roodash';

const data = {
  name: 'roodash',
  age: 1,
}

const options = {
  map: {
    name: 'title',
    age: 'years',
  },
}
const result = formatData(data, options)
console.log(result);
// => {title: 'roodash', years: 1}

const options = {
    map: {
        name: 'title',
        age: 'years',
    },
    keep: true,
    extra: {
        version: '1.3.0',
    },
}

const result = formatData(data, options);
console.log(result);
// => {
//     name: 'roodash',
//     age: 1,
//     title: 'roodash',
//     years: 1,
//     version: '1.3.0',
// }

const result = formatData(data, options);
console.log(result);
// => [{
//     title: 'roodash',
//     years: 1,
//     version: '1.3.0',
// }]

参数

属性说明类型默认值版本是否必填
data传入的数据对象或数组Record<string, any> | Record<string, any>[]-1.3.0
options可选参数FormatDataOptions{}1.3.0

FormatDataOptions

属性说明类型默认值版本是否必填
map映射关系Record<string, string>{}1.3.0
keep是否保留原始键booleanfalse1.3.0
extra额外的数据Record<string, any>{}1.3.0
deep是否深度格式化booleanfalse1.4.0
deepKey深度格式化keystringchildren1.4.0
deepKeyMap深度格式化 key 映射值stringchildren1.5.0

基于 MIT 协议发布