用递归将平行数据结构转换成树形数据结构(无id情况下)

1,测试数据 转换前
 let list = [
        {
          college: "梦想的学院",
          grade: "一年级",
          clazz: "1班",
        },
        {
          college: "嗷嗷叫学院",
          grade: "二年级",
          clazz: "1班",
          id: "xxx",
        },
        {
          college: "嗷嗷叫学院",
          grade: "一年级",
          clazz: "1班",
          id: "biubiubiu",
        },
        {
          college: "慢羊羊村大学",
          grade: "一年级",
          clazz: "1班",
        },
        {
          college: "梦想的学院",
          grade: "二年级",
          clazz: "1班",
        },
        {
          college: "慢羊羊村大学",
          grade: "一年级",
          clazz: "1班",
        },
      ];

2,转换后的树形结构
    [
        {
          title: "梦想的学院",
          children: [
            {
              title: "一年级",
              children: [
                {
                  title: "1班",
                  children: [
                    { college: "梦想的学院", grade: "一年级", clazz: "1班" },
                  ],
                },
              ],
            },
            {
              title: "二年级",
              children: [
                {
                  title: "1班",
                  children: [
                    { college: "梦想的学院", grade: "二年级", clazz: "1班" },
                  ],
                },
              ],
            },
          ],
        },
        {
          title: "嗷嗷叫学院",
          children: [
            {
              title: "二年级",
              children: [
                {
                  title: "1班",
                  children: [
                    {
                      college: "嗷嗷叫学院",
                      grade: "二年级",
                      clazz: "1班",
                      id: "xxx",
                    },
                  ],
                },
              ],
            },
            {
              title: "一年级",
              children: [
                {
                  title: "1班",
                  children: [
                    {
                      college: "嗷嗷叫学院",
                      grade: "一年级",
                      clazz: "1班",
                      id: "biubiubiu",
                    },
                  ],
                },
              ],
            },
          ],
        },
        {
          title: "慢羊羊村大学",
          children: [
            {
              title: "一年级",
              children: [
                {
                  title: "1班",
                  children: [
                    { college: "慢羊羊村大学", grade: "一年级", clazz: "1班" },
                    { college: "慢羊羊村大学", grade: "一年级", clazz: "1班" },
                  ],
                },
              ],
            },
          ],
        },
      ];


3、使用递归实现
递归实现的代码
      //   形成条件
      const winCondition = ["college", "grade", "clazz"];
      //   函数
      function data2TreeDigui(list, condition) {
        const newArr = [];
        condition = JSON.parse(JSON.stringify(condition));
        let order = condition.shift();
        let holdArr = [];
        const deep = condition.length;
        list.forEach((item, index) => {
          const holdIndex = holdArr.indexOf(item[order]);
          if (holdIndex > -1) {
            newArr[holdIndex]["children"].push(item);
            newArr[holdIndex]["title"] = item[order];
          } else {
            holdArr.push(item[order]);
            newArr.push({
              title: item[order],
              children: new Array(item),
            });
          }
        });
        newArr.forEach((item, index) => {
          if (item.children.length > 0 && deep) {
            //   给children赋值 递归
            item.children = data2TreeDigui(item.children, condition);
          }
        });
        return newArr;
      }
      //   结果
      const treeData = data2TreeDigui(list, winCondition);
      console.log("treeData: ", JSON.stringify(treeData));




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值