Skip to content

对象操作

TIP

fnType 字段说明:由于对象操作直接修改会导致原来数据发生改变,默认设为 pure表示纯函数,没有副作用,它将返回一个新的对象,即不会修改到原来的数据。

clearEmptyParams

清除值为'' 或 null 的字段,常用于后台接口传参,清除无效字段

参数说明

参数描述类型默认值
params对象object
fnType纯函数(pure)/虚函数(effect)stringpure
isIncludeNull是否清除 nullbooleantrue

代码示例

js
const obj = { name: "", age: 18, gender: 0 };

const res = clearEmptyParams(obj);
console.log(res, obj); // {"age": 18, "gender": 0} {"name: "", age": 18, "gender": 0}

const res = clearEmptyParams(obj, "effect");
console.log(res, obj); // {"age": 18, "gender": 0} {age": 18, "gender": 0}
const obj = { name: "", age: 18, gender: 0 };

const res = clearEmptyParams(obj);
console.log(res, obj); // {"age": 18, "gender": 0} {"name: "", age": 18, "gender": 0}

const res = clearEmptyParams(obj, "effect");
console.log(res, obj); // {"age": 18, "gender": 0} {age": 18, "gender": 0}

setDefaultParams

给对象设置默认值,常用于接收后台接口数据并做回显

参数说明

参数描述类型默认值
params设置对象object
data获取对象object
fnType纯函数(pure)/虚函数(effect)stringpure

代码示例

js
const obj = { name: "", age: 18, gender: 0 };
const defaultObj = { name: "王花花", age: 20, like: "code" };

const res = setDefaultParams(obj, defaultObj);
console.log(res); // {name: '王花花', age: 20, gender: 0}
const obj = { name: "", age: 18, gender: 0 };
const defaultObj = { name: "王花花", age: 20, like: "code" };

const res = setDefaultParams(obj, defaultObj);
console.log(res); // {name: '王花花', age: 20, gender: 0}

replaceKeys

替换对象键名,传入需要更换的对象和一个字段对应的 map,返回替换结果

参数说明

参数描述类型默认值
params设置对象object
keysMap替换键名:{oldKey: newKey}object{}
fnTypepure/effectstringpure

代码示例

js
const obj = { name: "", age: 18, gender: 0 };

const res = replaceKeys(obj, { name: "userName", gender: "sex" });
console.log(res); // {age: 18, userName: '', sex: 0}
const obj = { name: "", age: 18, gender: 0 };

const res = replaceKeys(obj, { name: "userName", gender: "sex" });
console.log(res); // {age: 18, userName: '', sex: 0}

jsonClone

通过 JSON.parse(JSON.stringify())方式克隆

js
const obj = { name: "", age: 18, gender: 0 };
jsonClone(obj);
const obj = { name: "", age: 18, gender: 0 };
jsonClone(obj);

deepClone

递归深拷贝

参数说明

参数描述类型默认值
target克隆目标对象object
type类型:obj/arrstringobj

代码示例

js
const res = deepClone(obj);

console.log(obj, res, obj == res); // {name: '', age: 18, gender: 0} {name: '', age: 18, gender: 0} false
const res = deepClone(obj);

console.log(obj, res, obj == res); // {name: '', age: 18, gender: 0} {name: '', age: 18, gender: 0} false

mergeParams

合并对象

参数说明

参数描述类型默认值
tar1对象 1object
tar2对象 2object

代码示例

js
const tar1 = { name: "王花花", age: 18, gender: 0 };
const tar2 = { name: "黎明花", age: 20, like: "code" };

const res = mergeParams(tar1, tar2); // {name: '黎明花', age: 20, gender: 0, like: 'code'}
const tar1 = { name: "王花花", age: 18, gender: 0 };
const tar2 = { name: "黎明花", age: 20, like: "code" };

const res = mergeParams(tar1, tar2); // {name: '黎明花', age: 20, gender: 0, like: 'code'}

pickParams

选出指定字段值,并返回一个新对象

参数说明

参数描述类型默认值
params克隆目标对象object
keyArr键名集合array

代码示例

js
const obj = { name: "王花花", age: 18, gender: 0 };
const res = pickParams(obj, ["name", "age"]); // {name: '王花花', age: 18}
const obj = { name: "王花花", age: 18, gender: 0 };
const res = pickParams(obj, ["name", "age"]); // {name: '王花花', age: 18}