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

相关推荐
wgc2k10 分钟前
NestJS基础-7: 官方 CLI 完整使用指南
前端
AI_零食19 分钟前
HarmonyOS-鸿蒙原生 ArkTS 布局系统:width(‘100%‘) 的本质与 padding 陷阱
前端·学习·华为·harmonyos·鸿蒙
英俊潇洒美少年22 分钟前
React18 flushSync 完整深度解析
前端·react
小鱼程序员25 分钟前
Reqable关于路径定位
前端
weixin_4462608526 分钟前
Typora 插件开发实战:基于 JavaScript/HTML 构建定制化 Markdown 扩展
开发语言·javascript·html
如烟花的信页27 分钟前
某管理服务平台点选逆向分析
javascript·爬虫·python·js逆向
梦曦i30 分钟前
Vite 0.1.7:构建追踪与资源映射新升级
前端
qq43569470133 分钟前
Vue02
开发语言·前端·javascript
AsiaLYF40 分钟前
Kotlin MutableSharedFlow: emit vs tryEmit 详解
开发语言·前端·kotlin
喜欢踢足球的老罗40 分钟前
Chrome MV3 插件架构深度解析:Service Worker 生命周期与 Token 管理的三层博弈
前端·chrome·架构