tree数据结构处理

import React from 'react';
interface RootObject {
  createUserId?: any;
  createUserName?: any;
  createTime?: any;
  updateUserId?: any;
  updateUserName?: any;
  updateTime?: any;
  moduleId: string;
  moduleName: string;
  parentId: string;
  moduleType: number;
  showOrder?: any;
  sysPermList: any[];
  sysPermModuleVOS: SysPermModuleVO2[];
}

interface SysPermModuleVO2 {
  createUserId?: any;
  createUserName?: any;
  createTime?: any;
  updateUserId?: any;
  updateUserName?: any;
  updateTime?: any;
  moduleId: string;
  moduleName: string;
  parentId: string;
  moduleType: number;
  showOrder?: any;
  sysPermModuleVOS: SysPermModuleVO[];
  sysPermList: any[];
}

interface SysPermModuleVO {
  createUserId?: any;
  createUserName?: any;
  createTime?: any;
  updateUserId?: any;
  updateUserName?: any;
  updateTime?: any;
  moduleId: string;
  moduleName: string;
  parentId: string;
  moduleType: number;
  showOrder?: any;
  sysPermModuleVOS: any[];
  sysPermList: any[];
}
export default function ceshi() {
  const downData: RootObject[] = [
    {
      createUserId: null,
      createUserName: null,
      createTime: null,
      updateUserId: null,
      updateUserName: null,
      updateTime: null,
      moduleId: '1630464368070103040',
      moduleName: '夹具需求',
      parentId: '0',
      moduleType: 1,
      showOrder: null,
      sysPermModuleVOS: [
        {
          createUserId: null,
          createUserName: null,
          createTime: null,
          updateUserId: null,
          updateUserName: null,
          updateTime: null,
          moduleId: '1630464565793787904',
          moduleName: '夹具需求',
          parentId: '1630464442514804736',
          moduleType: 1,
          showOrder: null,
          sysPermModuleVOS: [
            {
              createUserId: null,
              createUserName: null,
              createTime: null,
              updateUserId: null,
              updateUserName: null,
              updateTime: null,
              moduleId: '1630464614846173184',
              moduleName: '夹具需求',
              parentId: '1630464565793787904',
              moduleType: 1,
              showOrder: null,
              sysPermModuleVOS: [],
              sysPermList: []
            }
          ],
          sysPermList: []
        }
      ],
      sysPermList: []
    }
  ];

  // function treeInit(down:RootObject[]) {
  //   const treeData:{
  //     title: string;
  //     key: string;
  //     children: {
  //       title: string;
  //       key: string;
  //     }[];
  // }[] = [];
  //   let snap = 0;
  //   while (snap < down.length) {
  //     const {moduleName, parentId, moduleId} = down[snap];
  //     treeData[snap] = {
  //         title: moduleName,
  //         key: parentId == '0' ? moduleId : (parentId + '-' + moduleId),
  //         children: [],
  //     };
  //     if (down[snap].sysPermModuleVOS.length > 0) treeData[snap].children = treeInit(down[snap].sysPermModuleVOS);
  //     snap++;
  //   }
  //   return treeData;
  // }
  // console.log(treeInit(downData));
  function treeInit(arr: RootObject[], list:any[]) {
    for (let i of arr) {
      const {moduleName,parentId,moduleId,sysPermModuleVOS} =i
      list.push({
        title: moduleName,
        key: parentId == '0' ? moduleId : parentId + '-' + moduleId,
        children: []
      });
      for (let  a of list) {
        a.children = []
        treeInit(sysPermModuleVOS,a.children)
        if (a.children.length===0) delete a.children;
      }
    }
   
    return list
  }
  console.log(treeInit(downData,[]));
  return <div>ceshi</div>;
}

  

posted @ 2023-03-01 10:23  zjxgdq  阅读(13)  评论(0编辑  收藏  举报