Skip to content

引入

ts
import {
  deepSet,
  toHumpObj,
  extend
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  deepSet,
  toHumpObj,
  extend
} from 't-comm/lib/base/object/index';

// 只支持 ESM 的项目
import {
  deepSet,
  toHumpObj,
  extend
} from 't-comm/es/base/object/index';

deepSet(keyStr, target, value)

描述

深度赋值

参数

参数名描述
keyStr

以点拼接的 key,比如 foo.bar

target

目标对象

value

目标值

示例

ts
const obj = { a: { b: 1 } };
deepSet('a.c', obj, 2);

console.log(obj);
// { a: { b: 1, c: 2 } }

toHumpObj(obj)

描述

将对象中的key由下划线专为驼峰

参数

参数名类型描述
objobject

对象

返回: object

转化后的对象

示例

typescript
const obj = {
  a_a: 'a',
  b_b: [
    {
      bb_b: 'b',
    },
  ],
  c: {
    dd_d: 'd',
    e: {
      ee_e: 'e',
    },
  },
};

toHumpObj(obj);
// { aA: 'a', bB: [ { bbB: 'b' } ], c: { ddD: 'd', e: { eeE: 'e' } } }

extend(to, from)

描述

将属性混合到目标对象中

参数

参数名类型描述
toobject

目标对象

fromobject

原始对象

返回:

处理后的对象

示例

typescript
const a = { name: 'lee' }
const b = { age: 3 }
extend(a, b)

console.log(a)

// => { name: 'lee', age: 3 }