function treeConvertToArr(arr) {
let res = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].children) {
res = res.concat(this.treeConvertToArr(arr[i].children));
//删除父级的children属性,须保留children则把这行删除
delete arr[i].children
}
res.push(arr[i]);
}
return res;
}
const jsdata = [
{
"parentName": null,
"label": "合肥市",
"value": "340100",
"rawData": {
"parentName": "",
"name": "合肥市",
"value": "340100"
},
"childOpen": true,
"selected": false,
"isSearch": false,
"searchConnectives": "",
"parantCode": [
null
],
"level": 0,
"children": [
{
"parentName": "合肥市",
"label": "瑶海区",
"value": "340102",
"rawData": {
"parentName": "合肥市",
"name": "瑶海区",
"value": "340102"
},
"childOpen": true,
"selected": false,
"isSearch": false,
"searchConnectives": "",
"parantCode": [
"合肥市"
],
"level": 1,
"children": [
{
"parentName": "瑶海区",
"label": "新站",
"value": "3401023",
"rawData": {
"parentName": "瑶海区",
"name": "新站",
"value": "3401023"
},
"childOpen": true,
"selected": false,
"isSearch": false,
"searchConnectives": "",
"parantCode": [
"瑶海区"
],
"level": 2,
"children": [
{
"parentName": "新站",
"label": "磨店",
"value": "34010235",
"rawData": {
"parentName": "新站",
"name": "磨店",
"value": "34010235"
},
"childOpen": false,
"selected": false,
"isSearch": false,
"searchConnectives": "",
"parantCode": [
"新站"
],
"level": 3
}
]
}
]
}
]
}
]
console.log(treeConvertToArr(jsdata))
console