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

相关推荐
whyutianict_vv17 分钟前
从 Web 前端到 HarmonyOS ArkTS:一次 AI 鸿蒙全栈智能体开发的迁移实录
前端·人工智能·harmonyos
qziovv1 小时前
前端转flutter——项目架构、初始化
前端·flutter
Ali885201 小时前
Python字符串方法速查表大全
前端·python
前端_刘师兄1 小时前
FAE工程师学习路线-进程
前端
冰暮流星1 小时前
javascript之webstorage用法
开发语言·javascript·ecmascript
执子念的飞鱼2 小时前
浏览器直接预览 Pages、Numbers、Keynote:iWork 格式真正难在哪
前端·javascript
alloc2 小时前
从延迟聚合到可解释诊断:MetricKit 的原理与工程化实践
前端
颜进强2 小时前
06 - OpenSpec change 从模糊想法到完整契约:new change / explore / propose 三连
前端·后端·ai编程
Cache技术分享2 小时前
499. Java 反射 - 获取类型上的注解
前端·后端
用户921080262862 小时前
左侧历史对话模块:使用 Conversations 搭建会话入口
前端