//封装深拷贝
copyAry(options) {
return this.copy(options);
},
copy(ary) {
let newAry = [];
for (const item of ary) {
let value = item;
if (Object.prototype.toString.call(value) === "[object Object]")
value = this.copyObj(value);
if (Object.prototype.toString.call(value) === "[object Array]")
value = this.copyAry(value);
newAry.push(value);
}
return newAry;
},
copyObj(obj) {
let newObj = {};
for (const key in obj) {
let value = obj[key];
if (Object.prototype.toString.call(value) === "[object Object]")
value = this.copyObj(value);
if (Object.prototype.toString.call(value) === "[object Array]")
value = this.copyAry(value);
newObj[key] = value;
}
return newObj;
},
封装获取对象层数
getMaxFloor(treeData, num) {
let level = 0;
let v = this;
let maxLevel = 0;
function loop(data, level) {
data.forEach((item) => {
item.level = level;
if (level > maxLevel) {
maxLevel = level;
}
if ("children" in item) {
if (item.children.length > 0) {
loop(item.children, level + 1);
}
}
});
}
loop(treeData, 1);
this.power = maxLevel; //菜单层级数赋值
console.log(this.power)
},
注意:也可以使用json转换的格式进行深拷贝、
let a = JSON.parse(JSON.stringify(b))