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

相关推荐
king王一帅12 分钟前
啃完几十份国标,QuireKit 实现了像 Word 一样流畅的浏览器 OFD 编辑器,支持完备电子签标准与 OFD-H
前端·typescript·产品
菩提树下的凡夫12 分钟前
Web前端常见问题及解答
前端
map_3d_vis20 分钟前
百度地图添加标注 Marker 和自定义图标(JSAPI 4.0)
前端·javascript·地图·百度地图
码云数智-园园28 分钟前
HTML5 FileReader 读取本地文件,前端上传预览原理
前端·html·html5
恋猫de小郭32 分钟前
Flutter Analyzer ,这玩意还有这么大的优化空间
android·前端·flutter
夕水41 分钟前
夕水文学网502 故障 —— 爬虫把 Node 堆内存吃爆
javascript·后端·node.js
honkun61 小时前
vue 表格组件 vxe-table 行拖拽排序时,为“拖拽为子级”增加操作提示
前端·javascript·vue.js·vxe-table
qinqinzqq1 小时前
CSS Grid 命名区域布局:组件 Header / Content / Sider 两栏布局实践
前端·css
斯内普吖1 小时前
(开源)智慧物业管理系统实战指南 基于 Python + Django + uni-app + Vue + MySQL
vue.js·python·django·uni-app·开源
总要学点什么1 小时前
第127篇:河道流场水面——把整条河的流向烤进一张贴图
开发语言·javascript·microsoft·材质·贴图·cesium