提交 f816432f authored 作者: 姚鑫国's avatar 姚鑫国

优化 upObjKey

上级 a08c7889
......@@ -720,8 +720,9 @@ function setObjectKey(data) {
* @param {*} setKey {"被修改的key":"修改后的key"}
* @returns
*/
export function upObjKey(obj, setKey = {}) {
export function upObjKey(obj, setKey = {},defaultValue="") {
try {
obj = deepCopy(obj);
let objType = Object.prototype.toString.call(obj);
if (obj && (objType.indexOf("Array") > -1 || objType.indexOf("Object") > -1) && Object.keys(setKey).length > 0) {
if (objType === "[object Object]" && Object.keys(obj).length > 0) {
......@@ -729,7 +730,7 @@ export function upObjKey(obj, setKey = {}) {
let upKey = setKey[e];
let { newKey = e, type, timeFormat } = getSetKeyValue(upKey);
obj = getSetKeyJson({
obj, e, newKey, type, timeFormat
obj, e, newKey, type, timeFormat,defaultValue
});
}
}
......@@ -739,7 +740,7 @@ export function upObjKey(obj, setKey = {}) {
let upKey = setKey[e];
let { newKey = e, type, timeFormat } = getSetKeyValue(upKey);
arr[i] = getSetKeyJson({
obj: v, e, newKey, type, timeFormat
obj: v, e, newKey, type, timeFormat,defaultValue
});
}
})
......@@ -748,7 +749,7 @@ export function upObjKey(obj, setKey = {}) {
} else {
return null;
}
} catch (error) {
} catch (error) {
console.log("upObjKey",error);
return undefined;
}
......@@ -777,16 +778,17 @@ function getSetKeyValue(value) {
}
}
function getSetKeyJson({
obj, e, newKey, type, timeFormat
obj, e, newKey, type, timeFormat,defaultValue
}) {
obj = deepCopy(obj);
let value = obj[e];
delete obj[e];
switch (type) {
case "time":
obj[newKey] = timeFormat && value ? moment(value).format(timeFormat) : value || "";
obj[newKey] = (timeFormat && value ? moment(value).format(timeFormat) : value) || defaultValue;
break;
default:
obj[newKey] = value || "";
obj[newKey] = value || defaultValue;
}
return obj;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论