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

相关推荐
古夕1 分钟前
如何将异步操作封装为Promise
前端·javascript
小小小小宇2 分钟前
前端定高和不定高虚拟列表
前端
@一枝梅8 分钟前
vue3 vite.config.js 引入bem.scss文件报错
javascript·rust·vue·scss
古夕12 分钟前
JS 模块化
前端·javascript
wandongle13 分钟前
HTML面试整理
前端·面试·html
liucan23313 分钟前
JS执行速度似乎并不比Swift或者C语言慢
前端·ios
一只小风华~15 分钟前
HTML前端开发:JavaScript 常用事件详解
前端·javascript·html
Revol_C18 分钟前
【调试日志】我只是用wangeditor上传图片而已,页面咋就崩溃了呢~
前端·vue.js·程序员
天天码行空21 分钟前
GruntJS-前端自动化任务运行器从入门到实战
前端
smallzip22 分钟前
node大文件拷贝优化(显示进度)
前端·性能优化·node.js