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)
            }
          }
        },
     
     
  • 相关阅读:
    hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
    HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
    Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
    Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
    Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
    Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
    HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
    axios 简介与安装
    serializer 功能
    APIview 整理
  • 原文地址:https://www.cnblogs.com/fqs123456/p/11697994.html
Copyright © 2011-2022 走看看