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博客

相关推荐
q***76663 分钟前
SpringSecurity 实现token 认证
android·前端·后端
llxxyy卢4 分钟前
xss-maps(1-12)尝试思路过关加源码分析
前端·xss
Amy_yang6 分钟前
js 封装时间格式化,将单位有秒(s)的数据转换为'00:00:00'格式
javascript
interception7 分钟前
爬虫js逆向,jsdom补环境,抖音,a_bogus
javascript·爬虫·python
小璞8 分钟前
一、React Fiber 架构与任务调度详解
前端·react.js·前端框架
小璞8 分钟前
四、虚拟 DOM 与 Diff 算法:架构设计的智慧
前端·react.js·前端框架
南蓝9 分钟前
【AI 日记】调用大模型的时候如何按照 sse 格式输出
前端·人工智能
一树论11 分钟前
浏览器插件开发经验分享二:如何处理日期控件
前端·javascript
小璞11 分钟前
六、React 并发模式:让应用"感觉"更快的架构智慧
前端·react.js·架构
Yanni4Night12 分钟前
LogTape:零依赖的现代JavaScript日志解决方案
前端·javascript