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

相关推荐
兰德里的折磨5501 分钟前
基于若依和elementui实现文件上传(导入Excel表)
前端·elementui·excel
喝拿铁写前端4 分钟前
一个列表页面,初级中级高级前端之间的鸿沟就显出来了
前端·架构·代码规范
编程毕设1 小时前
【开题报告+论文+源码】基于SpringBoot+Vue的招聘管理系统的设计与实现
vue.js·spring boot·后端
magic 2451 小时前
ES6变量声明:let、var、const全面解析
前端·javascript·ecmascript·es6
M_chen_M1 小时前
es6学习02-let命令和const命令
前端·学习·es6
好_快1 小时前
Lodash源码阅读-dropWhile
前端·javascript·源码阅读
M_chen_M1 小时前
JS6(ES6)学习01-babel转码器
前端·学习·es6
好_快1 小时前
Lodash源码阅读-dropRightWhile
前端·javascript·源码阅读
二川bro2 小时前
Vue 项目中 package.json 文件的深度解析
前端·vue.js·json
寰宇视讯2 小时前
铼赛智能Edge mini斩获2025法国设计大奖 | 重新定义数字化齿科美学
前端·数据库·edge