/* * @Author: code4eat awesomedema@gmail.com * @Date: 2023-02-20 14:31:06 * @LastEditors: code4eat awesomedema@gmail.com * @LastEditTime: 2023-07-06 15:10:14 * @FilePath: /BudgetManaSystem/src/utils/tooljs.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ let parent:any = undefined; export const getDeepestTreeData:any= (tree:any,childrenName:string)=>{ if(tree[`${childrenName}`]&&tree[`${childrenName}`].length>0){ parent = tree; return getDeepestTreeData(tree[`${childrenName}`][0],childrenName) } return [tree,parent] } /* * @param x {Object} 对象1 * @param y {Object} 对象2 * @return {Boolean} true 为相等,false 为不等 */ export const deepEqual = (x: { [x: string]: any; } | null, y: { [x: string]: any; hasOwnProperty?: any; } | null) => { // 指向同一内存时 if (x === y) { return true; } else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) { if (Object.keys(x).length !== Object.keys(y).length) { return false; } for (var prop in x) { if (y.hasOwnProperty(prop)) { if (!deepEqual(x[prop], y[prop])) return false; } else { return false; } } return true; } else { return false; } } // 查找父节点的函数 interface Node { code: string; parentCode: number; children?: Node[]; } export const findAllParents = (data: Node[], parentCode:string): Node[] => { let result: Node[] = []; function findNodeRecursively(node: Node, path: Node[]): boolean { if (node.code === `${parentCode}`) { result = [...path, node]; return true; } if (node.children) { for (const child of node.children) { if (findNodeRecursively(child, [...path, node])) { return true; } } } return false; } for (const node of data) { if (parentCode != '0' && findNodeRecursively(node, [])) { break; } } return result; } //获取所有children节点的code interface TreeNode { code: string; children?: TreeNode[]; } export const getNodesWithChildren = (tree: TreeNode[]): TreeNode[] => { let nodes: TreeNode[] = []; function helper(node: TreeNode) { if (node.children && node.children.length > 0) { // 当前节点包含 children,添加整个节点 nodes.push(node); // 递归处理子节点 for (const child of node.children) { helper(child); } } } // 在这个版本中,我们对每个根节点进行操作 for (const node of tree) { helper(node); } return nodes; } //找出所有父节点的code export const findParentCodes = (node: TreeNode, parentCodes: string[] = []): string[] => { if (node.children && node.children.length > 0) { parentCodes.push(node.code); for (let child of node.children) { findParentCodes(child, parentCodes); } } return parentCodes; }