tooljs.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2023-02-20 14:31:06
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2023-07-06 15:10:14
  6. * @FilePath: /BudgetManaSystem/src/utils/tooljs.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. let parent:any = undefined;
  10. export const getDeepestTreeData:any= (tree:any,childrenName:string)=>{
  11. if(tree[`${childrenName}`]&&tree[`${childrenName}`].length>0){
  12. parent = tree;
  13. return getDeepestTreeData(tree[`${childrenName}`][0],childrenName)
  14. }
  15. return [tree,parent]
  16. }
  17. /*
  18. * @param x {Object} 对象1
  19. * @param y {Object} 对象2
  20. * @return {Boolean} true 为相等,false 为不等
  21. */
  22. export const deepEqual = (x: { [x: string]: any; } | null, y: { [x: string]: any; hasOwnProperty?: any; } | null) => {
  23. // 指向同一内存时
  24. if (x === y) {
  25. return true;
  26. } else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
  27. if (Object.keys(x).length !== Object.keys(y).length) {
  28. return false;
  29. }
  30. for (var prop in x) {
  31. if (y.hasOwnProperty(prop)) {
  32. if (!deepEqual(x[prop], y[prop])) return false;
  33. } else {
  34. return false;
  35. }
  36. }
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. // 查找父节点的函数
  43. interface Node {
  44. code: string;
  45. parentCode: number;
  46. children?: Node[];
  47. }
  48. export const findAllParents = (data: Node[], parentCode:string): Node[] => {
  49. let result: Node[] = [];
  50. function findNodeRecursively(node: Node, path: Node[]): boolean {
  51. if (node.code === `${parentCode}`) {
  52. result = [...path, node];
  53. return true;
  54. }
  55. if (node.children) {
  56. for (const child of node.children) {
  57. if (findNodeRecursively(child, [...path, node])) {
  58. return true;
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. for (const node of data) {
  65. if (parentCode != '0' && findNodeRecursively(node, [])) {
  66. break;
  67. }
  68. }
  69. return result;
  70. }
  71. //获取所有children节点的code
  72. interface TreeNode {
  73. code: string;
  74. children?: TreeNode[];
  75. }
  76. export const getNodesWithChildren = (tree: TreeNode[]): TreeNode[] => {
  77. let nodes: TreeNode[] = [];
  78. function helper(node: TreeNode) {
  79. if (node.children && node.children.length > 0) {
  80. // 当前节点包含 children,添加整个节点
  81. nodes.push(node);
  82. // 递归处理子节点
  83. for (const child of node.children) {
  84. helper(child);
  85. }
  86. }
  87. }
  88. // 在这个版本中,我们对每个根节点进行操作
  89. for (const node of tree) {
  90. helper(node);
  91. }
  92. return nodes;
  93. }
  94. //找出所有父节点的code
  95. export const findParentCodes = (node: TreeNode, parentCodes: string[] = []): string[] => {
  96. if (node.children && node.children.length > 0) {
  97. parentCodes.push(node.code);
  98. for (let child of node.children) {
  99. findParentCodes(child, parentCodes);
  100. }
  101. }
  102. return parentCodes;
  103. }