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 小时前
推荐一种适合前端开发使用的解决本地跨域问题的办法
前端
Niyy_2 小时前
前端一个工程构建多个项目,记录一次工程搭建
前端·javascript
xiangxiongfly9153 小时前
CSS link标签
前端·css
快乐非自愿4 小时前
常用设计模式:工厂方法模式
javascript·设计模式·工厂方法模式
岁月宁静4 小时前
AI 多模态全栈应用项目描述
前端·vue.js·node.js
十年磨一剑~4 小时前
html+js开发一个测试工具
javascript·css·html
nn_(nana)5 小时前
修改文件权限--- chmod ,vi/vim,查看文件内容,yum-软件包管理器,systemctl管理系统服务
前端
汪汪队立大功1235 小时前
JavaScript是怎么和html元素关联起来的?
开发语言·javascript·html
烛阴5 小时前
从零开始掌握C#核心:变量与数据类型
前端·c#