Vue tree树状结构数据转扁平数据

javascript 复制代码
//数据结构可参考饿了么UI
 treeData: [{
        id: 1,
        label: 'Level one 1',
        type: 1,
        children: [{
          id: 4,
          label: 'Level two 1-1',
          type: 2,
          children: [{
            id: 9,
            label: 'Level three 1-1-1',
            type: 3
          }, {
            id: 10,
            label: 'Level three 1-1-2',
            type: 3
          }]
        }, {
          id: 11,
          label: 'Level three 1-2',
          type: 2,
          children: [{
            id: 12,
            label: 'Level three 1-2-1',
            type: 3
          }, {
            id: 13,
            label: 'Level three 1-2-2',
            type: 3
          }, {
            id: 14,
            label: 'Level three 1-2-3',
            type: 3
          }, {
            id: 15,
            label: 'Level three 1-2-4',
            type: 3
          }]
        }]
      }]
javascript 复制代码
//转结构(list就是你要转的树状数据)
 treeToList(list) {
      let res = []
      for (const item of list) {
        const { children, ...i } = item
        if (children && children.length) {
          res = res.concat(this.treeToList(children))
        }
        res.push(i)
      }
      return res
    }
//查看数据是否转换成功
  const treeList = this.treeToList(this.treeData)
  console.log('树状结构转扁平结构', treeList)   

参考文档Vue tree树状结构数据转扁平数据_vue树形结构转化为平行结构-CSDN博客

相关推荐
启山智软2 分钟前
【启山智软智能商城系统技术架构剖析】
java·前端·架构
我命由我1234514 分钟前
React Router 6 - 嵌套路由、路由传递参数
前端·javascript·react.js·前端框架·html·ecmascript·js
十六年开源服务商43 分钟前
2026年WordPress网站地图完整指南
java·前端·javascript
GISer_Jing1 小时前
Agent架构师详解:Skill是什么?附CSDN博客撰写可复用Skill示例
前端·ai·aigc
liucan20121 小时前
nginx服务器实现上传文件功能_使用nginx-upload-module模块
服务器·前端·nginx
英俊潇洒美少年1 小时前
MessageChannel 如何实现时间切片
javascript·react.js·ecmascript
x-cmd2 小时前
[x-cmd] 一切 Web、桌面应用和本地工具皆可 CLI -opencli
前端·ai·github·agent·cli·x-cmd
南山love2 小时前
spring-boot项目实现发送qq邮箱
java·服务器·前端
F1FJJ2 小时前
Shield CLI:MySQL 插件 vs phpMyAdmin:轻量 Web 数据库管理工具对比
前端·网络·数据库·网络协议·mysql·容器
技术钱3 小时前
react数据大屏四种适配方案
javascript·react.js·ecmascript