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

优化 upObjKey

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