树状结构递归转变key值

2021/7/27 23:06:04

本文主要是介绍树状结构递归转变key值,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

转变前:

  "data": {
    "id": 7,
    "name": "河北农行",
    "code": "50H999",
    "fatherId": null,
    "fatherCode": "000000",
    "level": 1,
    "childBank": [
      {
        "id": 8,
        "name": "唐山分行",
        "code": "50H700",
        "fatherId": 8,
        "fatherCode": "50H999",
        "level": 2,
        "childBank": []
      },
      {
        "id": 9,
        "name": "廊坊分行",
        "code": "50H650",
        "fatherId": 9,
        "fatherCode": "50H999",
        "level": 2,
        "childBank": []
      },
      {
        "id": 10,
        "name": "秦皇岛分行",
        "code": "50H800",
        "fatherId": 10,
        "fatherCode": "50H999",
        "level": 2,
        "childBank": []
      },
      {
        "id": 30,
        "name": "石家庄分行",
        "code": "50H300",
        "fatherId": 30,
        "fatherCode": "50H999",
        "level": 2,
        "childBank": []
      }
    ]
  },

转变中:

    getBankTreeSelect () {
      getBankTreeSelect().then(res => {
        if (res.data) {
          const { data } = res.data
          let bankTreeData = data ? [data] : []
          const loop = (data) => {
            data.forEach((item, index) => {
              if (item.childBank.length) {
                data[index] = {
                  value: item.code,
                  label: item.name,
                  children: item.childBank
                }
              } else {
                data[index] = {
                  value: item.code,
                  label: item.name
                  // children: item.childBank,
                }
              }
              if (item.childBank && item.childBank.length) {
                loop(item.childBank)
              }
            })
          }
          loop(bankTreeData)
          this.bankTreeData = bankTreeData
          console.log(bankTreeData)
        }
      })
    }

转变后:



这篇关于树状结构递归转变key值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程