zoukankan      html  css  js  c++  java
  • 将接口数据通过递归过滤

    数据源:

    [
      {
        nodeId: "8d6d70cb44ea4172b4c6a3ab760330ea",
        node: {
          powerId: "8d6d70cb44ea4172b4c6a3ab760330ea",
          parentId: "0",
          orderIndex: 4,
          resourceType: "MENU",
          resourceName: "项目管理",
          level: null,
          resourceDesc: "",
          resourceUrl: "/projectManage",
          resourceIcon: "iconfont icon-xiangmu",
          moduleId: "ea3f471cc286450b8329e86e90fe24dc",
          isDeleted: false,
          createTime: "2020-06-22 18:45:26",
          updateTime: "2020-06-22 18:52:36",
        },
        parentId: "0",
        children: null,
        childLeaf: true,
      },
      {
        nodeId: "6088a9af2f514e5090ccbd51110424b5",
        node: {
          powerId: "6088a9af2f514e5090ccbd51110424b5",
          parentId: "0",
          orderIndex: 6,
          resourceType: "MENU",
          resourceName: "资料汇总",
          level: null,
          resourceDesc: "",
          resourceUrl: "/dataCollection",
          resourceIcon: "iconfont icon-ziliao",
          moduleId: "5ecf31302dff488f84620d5b816d98ee",
          isDeleted: false,
          createTime: "2020-06-22 18:45:26",
          updateTime: "2020-06-22 18:52:56",
        },
        parentId: "0",
        children: null,
        childLeaf: true,
      },
      {
        nodeId: "8621da3397374510a17b59ad9cfdca74",
        node: {
          powerId: "8621da3397374510a17b59ad9cfdca74",
          parentId: "0",
          orderIndex: 9,
          resourceType: "MENU",
          resourceName: "个人中心",
          level: null,
          resourceDesc: "",
          resourceUrl: "/personalCenter",
          resourceIcon: "iconfont icon-yonghu1",
          moduleId: "242026239a9e40879915867307364365",
          isDeleted: false,
          createTime: "2020-06-22 18:45:26",
          updateTime: "2020-06-22 18:53:19",
        },
        parentId: "0",
        children: null,
        childLeaf: true,
      },
      {
        nodeId: "1b4c695c711640949872510e02752b69",
        node: {
          powerId: "1b4c695c711640949872510e02752b69",
          parentId: "0",
          orderIndex: 52,
          resourceType: "MENU",
          resourceName: "伦理会议",
          level: null,
          resourceDesc: "",
          resourceUrl: "/ethicalMeeting",
          resourceIcon: "",
          moduleId: "52ad856eb1a84fbbb6f31ba59f93aba4",
          isDeleted: false,
          createTime: "2020-06-22 18:45:26",
          updateTime: "2020-06-22 18:45:26",
        },
        parentId: "0",
        children: [
          {
            nodeId: "79234bc38316477680a555f3db4a00dd",
            node: {
              powerId: "79234bc38316477680a555f3db4a00dd",
              parentId: "1b4c695c711640949872510e02752b69",
              orderIndex: 1,
              resourceType: "MENU",
              resourceName: "待会审项目",
              level: null,
              resourceDesc: "",
              resourceUrl: "/ethicalMeeting/pendingReviewProject'",
              resourceIcon: "",
              moduleId: "52ad856eb1a84fbbb6f31ba59f93aba4",
              isDeleted: false,
              createTime: "2020-06-22 18:45:26",
              updateTime: "2020-06-22 18:45:26",
            },
            parentId: "1b4c695c711640949872510e02752b69",
            children: null,
            childLeaf: true,
          },
          {
            nodeId: "5f38c08e24a64ba0bb9b3ee53f192eb2",
            node: {
              powerId: "5f38c08e24a64ba0bb9b3ee53f192eb2",
              parentId: "1b4c695c711640949872510e02752b69",
              orderIndex: 2,
              resourceType: "MENU",
              resourceName: "会议记录",
              level: null,
              resourceDesc: "",
              resourceUrl: "/ethicalMeeting/pendingReviewProject",
              resourceIcon: "",
              moduleId: "52ad856eb1a84fbbb6f31ba59f93aba4",
              isDeleted: false,
              createTime: "2020-06-22 18:45:26",
              updateTime: "2020-06-22 18:45:26",
            },
            parentId: "1b4c695c711640949872510e02752b69",
            children: null,
            childLeaf: true,
          },
        ],
        childLeaf: false,
      },
      {
        nodeId: "a8a55825d32f43fb8dac59bf3aeef0e3",
        node: {
          powerId: "a8a55825d32f43fb8dac59bf3aeef0e3",
          parentId: "0",
          orderIndex: 82,
          resourceType: "MENU",
          resourceName: "用户管理",
          level: null,
          resourceDesc: "",
          resourceUrl: "/userManage",
          resourceIcon: "iconfont icon-yonghuguanli",
          moduleId: "dc6804f659974d16bf5a498eceb65a25",
          isDeleted: false,
          createTime: "2020-06-22 18:45:26",
          updateTime: "2020-06-22 18:45:26",
        },
        parentId: "0",
        children: [
          {
            nodeId: "fdb9c937156148cc88060450933e2d72",
            node: {
              powerId: "fdb9c937156148cc88060450933e2d72",
              parentId: "a8a55825d32f43fb8dac59bf3aeef0e3",
              orderIndex: 1,
              resourceType: "MENU",
              resourceName: "本院用户",
              level: null,
              resourceDesc: "",
              resourceUrl: "/userManage/ourHospitalUser",
              resourceIcon: "",
              moduleId: "dc6804f659974d16bf5a498eceb65a25",
              isDeleted: false,
              createTime: "2020-06-22 18:45:26",
              updateTime: "2020-06-22 18:45:26",
            },
            parentId: "a8a55825d32f43fb8dac59bf3aeef0e3",
            children: null,
            childLeaf: true,
          },
        ],
        childLeaf: false,
      }
    ]
    View Code

    需要的结果格式:

    [
      {
        path: "/ethicsWorkbench",
        text: "伦理工作台",
        icon: "iconfont icon-yingyong1",
        children: [
          {
            path: "/ethicsWorkbench/chiefJudge",
            text: "审查任务(主审)",
          },
          {
            path: "/ethicsWorkbench/secretary",
            text: "审查任务(秘书)",
          },
          {
            path: "/ethicsWorkbench/director",
            text: "审查任务(主任)",
          },
        ],
      },
      {
        path: "/userManage",
        text: "用户管理",
        icon: "iconfont icon-yonghuguanli",
        children: [
          {
            path: "/userManage/ourHospitalUser",
            text: "本院用户",
          },
          {
            path: "/userManage/sponsorUser",
            text: "申办方用户",
          },
        ],
      },
      {
        path: "/personalCenter",
        text: "个人中心",
        icon: "iconfont icon-yonghu1",
      }
    ]

    递归:

          function fn(data, result = []) {
            result = data.map((item) => {
              if (item.children) item.children = fn(item.children, []);
              return {
                path: item.node.resourceUrl,
                text: item.node.resourceName,
                icon: item.node.resourceIcon,
                children: item.children || [],
              };
            });
            return result;
          }

    结果:

  • 相关阅读:
    [设计模式] 10 外观模式 facade
    [设计模式] 11 享元模式 Flyweight
    [设计模式] 9 装饰者模式 Decorator
    [设计模式] 8 组合模式 Composite
    float2int
    《ASP.NET》数据绑定——GridView
    Android Studio左边栏Project不见了?
    Linux下的五种I/O模型
    Shell编程入门(第二版)(下)
    Android訪问网络,使用HttpURLConnection还是HttpClient?
  • 原文地址:https://www.cnblogs.com/wuqilang/p/14728231.html
Copyright © 2011-2022 走看看