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

相关推荐
xkxnq5 分钟前
第二阶段:Vue 组件化开发(第 28天)
前端·javascript·vue.js
摘星编程13 分钟前
React Native for OpenHarmony 实战:RTL 从右到左布局详解
javascript·react native·react.js
Yvonne爱编码22 分钟前
前端工程化进阶:从搭建完整项目脚手架到性能优化【技术类】
前端·状态模式
cypking27 分钟前
二、前端规范化 遇到的问题及解决方案
前端
小范馆28 分钟前
STM32F03C8T6通过AT指令获取天气API
前端·javascript·stm32
zhengxianyi51541 分钟前
vue-cli build, vite build 生产部署刷新或弹窗404,页面空白修复方法
前端·javascript·vue.js·nginx·生产部署
Filotimo_43 分钟前
前端项目打包部署完整流程
前端
Savvy..1 小时前
Day15 Talis 前端
前端
恋爱绝缘体11 小时前
Vue.js 组件 - 自定义事件【1】
前端·javascript·vue.js
梦6501 小时前
JavaScript ES5 + ES6+ 字符串 (String) 所有方法大全
前端·javascript·es6