第一种没什么说的,就是尾递归会耗性能
var a = {
b: {
c: [{
c: 3
}]
},
e: [{
a: 2
}]
}
function deepClone(obj) {
let result = Array.isArray(obj) ? [] : {}
for (const key in obj) {
const thisValue = obj[key]
if (thisValue.constructor===Array||thisValue.constructor===Object) {
result[key] = deepClone(thisValue)
} else {
result[key] = thisValue
}
}
return result
}
let res=deepClone(a)
console.log(res);
console.log(res.b.c[0] === a.b.c[0]);
let cloneTempData = [
{
index: "1-0",
name: "name",
position: "PATH",
type: "String",
cls: "",
isRequired: false,
fixedValue: "",
defaultValue: "",
children: [
{
id: "add_temp_id",
name: "这里写参数名",
position: "",
cls: "",
type: "",
isRequired: "",
fixedValue: "",
defaultValue: "",
index: "0-0-0",
children: null,
},
{
id: "add_temp_id",
name: "这里写参数名",
position: "",
cls: "",
type: "",
isRequired: "",
fixedValue: "",
defaultValue: "",
index: "0-0-1",
children: [1, 23],
},
],
},
]
const thisData = {
parent: cloneTempData.constructor===Array?[]:{},
data: cloneTempData,
};
let t = [thisData];
while (t.length) {
const tdata = t.shift();
for (const key in tdata.data) {
const thisV=tdata.data[key]
const constructor = thisV && thisV.constructor;
let nextObj;
if (constructor === Array) {
nextObj = [...thisV];
} else if (constructor === Object) {
nextObj = { ...thisV };
} else {
nextObj = thisV;
}
tdata.parent[key] = nextObj;
if (typeof thisV === "object" && thisV) {
t.push({
parent: nextObj,
data: thisV,
});
}
}
}
console.log(thisData.parent[0].children,thisData.data[0].children);