<script>
// 递归?实现对象的深度复制
let obj = {
name:"杨亚坤",
yuwen:90,
childern:[
{name:"亚坤1",yuwen:80},
{name:"亚坤2",yuwen:800},
],
hoby:[1,2,3,[33,44]]
};
function deepCopy(obj){
let arrobj;
Array.isArray(obj)?arrobj =[] :arrobj = {};
for(var i in obj){
if(obj.hasOwnProperty(i)){
arrobj[i] = typeof obj[i] === "object" ? deepCopy(obj[i]) : obj[i];
}
}
return arrobj;
}
let resultObj = deepCopy(obj);
resultObj["name"]="yyyyyy";
console.log(resultObj);
console.log(obj);
// 递归实现 树状数组的推平
let objTree = {
name:"总部公司",
SelfId:1,
parentId:0,
children:[
{
name:"一层分公司1",
SelfId:2,
parentId:1,
children:[]
},
{
name:"一层分公司2",
SelfId:3,
parentId:1,
children:[
{
name:"二层分公司1",
SelfId:4,
parentId:3,
children:[]
}
]
},
]
};
let resultArray = [];
function deepChange(obj){
let myname = obj.name;
let SelfId = obj.SelfId;
let parentId = obj.parentId;
let children = obj.children;
resultArray.push({myname:myname,SelfId:SelfId,parentId:parentId});
if(children.length ==0){
return "";
}else{
for(var i = 0 ;i<children.length;i++){
deepChange(children[i]);
}
}
}
deepChange(objTree);
console.log(resultArray)
</script>