zoukankan      html  css  js  c++  java
  • 树形数据转json

    数据格式
     

    返回数据格式1: (最后一级children如果为空,则为children:[])

        transData(data) {
          const resData = data
          const tree = []
          for (let i = 0; i < resData.length; i++) {
            if (resData[i]['domainLevel'] === '1') {
              const obj = {
                domainCode: resData[i]['domainCode'],
                domainDescription: resData[i]['domainDescription'],
                domainLevel: resData[i]['domainLevel'],
                children:[]
              }
              tree.push(obj)
              resData.splice(i, 1)
              i--
            }
          }
          this.run(tree, resData)
          return tree
        },
        run(chiArr, resData) {      
            if (resData && resData.length !== 0) {
            for (let i = 0; i < chiArr.length; i++) {
              for (let j = 0; j < resData.length; j++) {
                if (resData[j]['domainCode'].substring(0, chiArr[i].domainCode.length) === chiArr[i].domainCode && resData[j]['domainLevel'] === (parseInt(chiArr[i].domainLevel) + 1).toString()) {
                  const obj = {
                    domainCode: resData[j]['domainCode'],
                    domainDescription: resData[j]['domainDescription'],
                    domainLevel: resData[j]['domainLevel'],
                    children: []
                  }
                  chiArr[i].children.push(obj)
                  resData.splice(j, 1)
                  j--
                }
              }
              this.run(chiArr[i].children, resData)
            }
          }
        }
     
    返回格式2:(最后一级children如果为空,则不要children)

        transData(data) {
          const resData = data
          const tree = []
          for (let i = 0; i < resData.length; i++) {
            if (resData[i]['domainLevel'] === '1') {
              const obj = {
                domainCode: resData[i]['domainCode'],
                domainDescription: resData[i]['domainDescription'],
                domainLevel: resData[i]['domainLevel']
              }
              tree.push(obj)
              resData.splice(i, 1)
              i--
            }
          }
          this.run(tree, resData)

          return tree
        },
        run(chiArr, resData) {
          for (let i = 0; i < chiArr.length; i++) {
            const temp = resData.filter(item => item.domainCode.substring(0, chiArr[i].domainCode.length) === chiArr[i].domainCode && item.domainLevel === (parseInt(chiArr[i].domainLevel) + 1).toString())
            if (temp && temp.length > 0) {
              chiArr[i].children = temp
            }
            if (chiArr[i].children) {
              this.run(chiArr[i].children, resData)
            }
          }
        },
     
     
  • 相关阅读:
    python实现布隆过滤器及原理解析
    gin框架源码解析
    阿里云docker操作问题记录
    Qt编写数据可视化大屏界面电子看板系统
    CSS3-3D制作案例分析实战
    前端可视化项目流程,涉及three.js(webGL),3DMax技术,持续更新
    前端可视化项目流程,涉及three.js(webGL),3DMax技术,持续更新
    jquery拖拽排序,针对后台列表table进行拖拽排序(Echart不刷新页面,多语言切换下的地图数据重新加载,api请求数据加载
    Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)C# 创建Excel气泡图
    Java 如何在PPT中设置形状组合、取消组合、编辑组合形状
  • 原文地址:https://www.cnblogs.com/fqs123456/p/11697994.html
Copyright © 2011-2022 走看看