[ { "name": "小明", "code": "0", "children": [ { "name": "小明明1", "code": "00", "children": [ { "name": "小明明明1", "code": "000", "id": 3, "prentId": 2 }, { "name": "小明明明2", "code": "001", "id": 4, "prentId": 2 } ], "id": 2, "prentId": 1 }, { "name": "小明明2", "code": "01", "id": 5, "prentId": 1 }, { "name": "小明明3", "code": "02", "id": 6, "prentId": 1 } ], "id": 1, "prentId": 1 }, { "name": "小红", "code": "1", "children": [ { "name": "小红红1", "code": "11", "id": 8, "prentId": 7 }, { "name": "小红红2", "code": "12", "id": 9, "prentId": 7 } ], "id": 7, "prentId": 7 } ]
function getTreeName(tree, obj, arr) { for (let i = 0; i < tree.length; i++) { let item = tree[i] if (obj[item.prentId]) { item.name = obj[item.prentId] + '-' + item.name } obj[item.id] = item.name if (item.children && item.children.length) { getTreeName(item.children, obj, arr) } else { arr.push(item.name) } } }