123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * @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;
- }
|