树状结构递归转变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值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-20RabbitMQ教程:新手入门指南
- 2024-11-20Redis教程:新手入门指南
- 2024-11-20SaToken教程:新手入门指南
- 2024-11-20SpringBoot教程:从入门到实践
- 2024-11-20Java全栈教程:从入门到实战
- 2024-11-20Java微服务系统教程:入门与实践指南
- 2024-11-20Less教程:初学者快速上手指南
- 2024-11-20MyBatis教程:新手快速入门指南
- 2024-11-20QLExpress教程:初学者快速入门指南
- 2024-11-20订单系统教程:从入门到实践的全面指南