JavaScript实现数组扁平化与深拷贝

数组扁平化与深拷贝工具方法

原始数据

const menu = [
  {
    group: "基本信息",
    title: "基本信息",
    path: "/Basemessage",
    meta: {
      icon: "../assets/img/navicon/icon02.png",
      iconColor: "../assets/img/navicon/icon12.png",
      haveColor: false
    },
    id: 2,
    component: "",
    children: [
      {
        group: "xxxx",
        title: "xxx",
        path: "Business",
        name: "Business",
        id: 2,
        component: ""
      },
      {
        group: "变更记录",
        title: "变更记录",
        path: "ChangeLog",
        name: "ChangeLog",
        id: 2,
        component: ""
      }
    ]
  },
  {
    group: "图谱",
    title: "图谱",
    path: "/CompanyFigure",
    meta: {
      icon: "../assets/img/navicon/icon03.png",
      iconColor: "../assets/img/navicon/icon13.png",
      haveColor: false
    },
    children: [
      {
        group: "企业",
        title: "企业",
        path: "CompanyChain",
        name: "CompanyChain",
      },
      {
        group: "关系",
        title: "关系",
        path: "BusinessRelation",
        name: "BusinessRelation",
      }
    ]
  },
  {
    group: "资信",
    title: "资信",
    path: "/Credit",
    meta: {
      icon: "../assets/img/navicon/icon04.png",
      iconColor: "../assets/img/navicon/icon14.png",
      haveColor: false
    },
    children: [
      {
        group: "aaaa",
        title: "aaa",
        path: "Default",
        name: "Default",
      },
      {
        group: "公告信息",
        title: "公告信息",
        path: "NoticeMsg",
        name: "NoticeMsg",
      }
    ]
  },
  {
    group: "集团",
    title: "集团",
    path: "/Group",
    meta: {
      icon: "../assets/img/navicon/icon05.png",
      iconColor: "../assets/img/navicon/icon15.png",
      haveColor: false
    },
    children: [
      {
        group: "集团成员",
        title: "集团成员",
        path: "GroupMember",
        name: "GroupMember",
      },
      {
        group: "新闻舆情",
        title: "新闻舆情",
        path: "NewsList",
        name: "NewsList",
      }
    ]
  },
  {
    group: "司法",
    title: "司法",
    path: "/Judicial",
    meta: {
      icon: "../assets/img/navicon/icon06.png",
      iconColor: "../assets/img/navicon/icon16.png",
      haveColor: false
    },
    children: [
      {
        group: "惩戒",
        title: "惩戒",
        path: "Punishment",
        name: "Punishment",
      }
    ]
  },
  {
    group: "融资",
    title: "融资",
    path: "/FinancingMessage",
    meta: {
      icon: "../assets/img/navicon/icon08.png",
      iconColor: "../assets/img/navicon/icon18.png",
      haveColor: false
    },
    children: [
      {
        group: "融资",
        title: "融资",
        path: "AccountsReceivableFinancing",
        name: "AccountsReceivableFinancing",
      },
      {
        group: "债券",
        title: "债券",
        path: "BondFinancing",
        name: "BondFinancing",
      },
      {
        group: "信托",
        title: "信托",
        path: "TrustFinancing",
        name: "TrustFinancing",
      },
      {
        group: "股权",
        title: "股权",
        path: "EquityPledge",
        name: "EquityPledge",
      },
    ]
  }
  
];

数组扁平化

 const flatten = (arr: any) => {
      let result: any = [];
      for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i].children)) {
          result = result.concat(flatten(arr[i].children));
        } else {
          result.push(arr[i]);
        }
      }
      return result;
    };

扁平化处理后

在这里插入图片描述

数组深拷贝

 const clone = (obj: any) => {
      let o: any;
      // 如果  他是对象object的话  , 因为null,object,array  也是'object';
      if (typeof obj === "object") {
        // 如果  他是空的话
        if (obj === null) {
          o = null;
        } else {
          // 如果  他是数组arr的话
          if (obj instanceof Array) {
            o = [];
            for (let i = 0, len = obj.length; i < len; i++) {
              o.push(clone(obj[i]));
            }
          }
          // 如果  他是对象object的话
          else {
            o = {};
            for (const j in obj) {
              o[j] = clone(obj[j]);
            }
          }
        }
      } else {
        o = obj;
      }
      return o;
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值