1. //封装深拷贝
  2. copyAry(options) {
  3. return this.copy(options);
  4. },
  5. copy(ary) {
  6. let newAry = [];
  7. for (const item of ary) {
  8. let value = item;
  9. if (Object.prototype.toString.call(value) === "[object Object]")
  10. value = this.copyObj(value);
  11. if (Object.prototype.toString.call(value) === "[object Array]")
  12. value = this.copyAry(value);
  13. newAry.push(value);
  14. }
  15. return newAry;
  16. },
  17. copyObj(obj) {
  18. let newObj = {};
  19. for (const key in obj) {
  20. let value = obj[key];
  21. if (Object.prototype.toString.call(value) === "[object Object]")
  22. value = this.copyObj(value);
  23. if (Object.prototype.toString.call(value) === "[object Array]")
  24. value = this.copyAry(value);
  25. newObj[key] = value;
  26. }
  27. return newObj;
  28. },

封装获取对象层数

  1. getMaxFloor(treeData, num) {
  2. let level = 0;
  3. let v = this;
  4. let maxLevel = 0;
  5. function loop(data, level) {
  6. data.forEach((item) => {
  7. item.level = level;
  8. if (level > maxLevel) {
  9. maxLevel = level;
  10. }
  11. if ("children" in item) {
  12. if (item.children.length > 0) {
  13. loop(item.children, level + 1);
  14. }
  15. }
  16. });
  17. }
  18. loop(treeData, 1);
  19. this.power = maxLevel; //菜单层级数赋值
  20. console.log(this.power)
  21. },

注意:也可以使用json转换的格式进行深拷贝、

let a = JSON.parse(JSON.stringify(b))