tree.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * 提取菜单树中的每一项path
  3. * @param {Object} {menuTree 菜单树}
  4. * @param {return}} expandedPaths 每一项path组成的数组
  5. */
  6. const expandedPaths = [];
  7. export function extractPathList(menuTree) {
  8. if (!Array.isArray(menuTree)) {
  9. console.warn("menuTree must be an array");
  10. return;
  11. }
  12. if (!menuTree || menuTree.length === 0) return;
  13. for (const node of menuTree) {
  14. const hasChildren = node.children && node.children.length > 0;
  15. if (hasChildren) {
  16. extractPathList(node.children);
  17. }
  18. expandedPaths.push(node.path);
  19. }
  20. return expandedPaths;
  21. }
  22. /**
  23. * 如果父级下children的length为1,删除children
  24. * @param {Object} {menuTree 菜单树}
  25. * @param {return}}
  26. */
  27. export function deleteChildren(menuTree) {
  28. if (!Array.isArray(menuTree)) {
  29. console.warn("menuTree must be an array");
  30. return;
  31. }
  32. if (!menuTree || menuTree.length === 0) return;
  33. for (const node of menuTree) {
  34. if (node.children && node.children.length === 1) delete node.children;
  35. const hasChildren = node.children && node.children.length > 0;
  36. if (hasChildren) {
  37. deleteChildren(node.children);
  38. }
  39. }
  40. return menuTree;
  41. }