转变前:
"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) } }) }
转变后: