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)
            }
          }
        },
     
     
  • 相关阅读:
    Spring MVC @RequestMapping注解详解
    (转)Cesium教程系列汇总
    spring boot +mybatis(通过properties配置) 集成
    SpringBoot2.0之四 简单整合MyBatis
    在Windows下使用Git+TortoiseGit+码云管理项目代码
    TortoiseGit之配置密钥
    Spring Boot 学习之路二 配置文件 application.yml
    SpringBoot学习笔记(2) Spring Boot的一些配置
    【入门】Spring-Boot项目配置Mysql数据库
    Spring 的application.properties项目配置与注解
  • 原文地址:https://www.cnblogs.com/fqs123456/p/11697994.html
Copyright © 2011-2022 走看看