这是一棵树:
"data": [
{
"children": [
{
"children": [
{
"pid": "2101",
"id": "32",
"label": "1#线吊点右",
"type": 3,
"value": "1#线吊点右"
},
{
"pid": "2101",
"id": "314",
"label": "川C7890",
"type": 3,
"value": "川C7890"
},
{
"pid": "2101",
"id": "315",
"label": "川C7891",
"type": 3,
"value": "川C7891"
},
{
"pid": "2101",
"id": "321",
"label": "9999川456734567890",
"type": 3,
"value": "9999川456734567890"
}
],
"pid": "11",
"id": "2101",
"label": "炼铁作业区",
"type": 2,
"value": "炼铁作业区"
}
],
"id": "11",
"label": "炼铁厂",
"type": 1,
"value": "炼铁厂"
},
{
"children": [
{
"children": [],
"pid": "12",
"id": "2202",
"label": "炼钢作业区",
"type": 2,
"value": "炼钢作业区"
}
],
"id": "12",
"label": "炼钢厂",
"type": 1,
"value": "炼钢厂"
}
]
这是我把它改写成这种形式 [ {}, {}, {}, {} …]
就是会用到递归
// 递归树结构
var list = []
var treeArrList = []
function reconstruct(data) {
data.forEach(a => {
list.push({
'pid': a.pid ? a.pid : '',
'id': a.id,
'label': a.label,
'type': a.type,
'value': a.value
})
if (a.children) {
var arr = a.children
reconstruct(arr)
}
})
}
// console.log('list:', list)
treeArrList = list
console.log('oo:', treeArrList)
reconstruct(this.treeData)