let mapObj = {
text: 'name',
value: 'id',
parentId: 'parent_id',
children: 'children'
}; //级联要求的数据格式 属性值是调接口得来的数据对应字段,此处不调接口,写死模拟一下
let data = [
{
name: '江苏省',
id: 1,
parent_id: 0,
children: [
{
name: '南京市',
id: 11,
parent_id: 1,
children: [
{
name: '秦淮区',
id: 111,
parent_id: 11,
children: [
]
},
{
name: '鼓楼区',
id: 112,
parent_id: 11,
children: [
]
},
]
},
{
name: '扬州市',
id: 12,
parent_id: 1,
children: [
{
name: '邗江区',
id: 121,
parent_id: 11,
children: [
]
}
]
},
]
},
{
name: '江西省',
id: 2,
parent_id: 0,
children: []
}
]
function converTree(tree, map) {
const result = [];
tree.forEach((item) => {
let value = item[ map.value ];
let text = item[ map.text ];
let parentId = item[map.parentId]
let children = item[ map.children ];
if (children) {
children = converTree(children, map);
}
result.push({ value, text, parentId, children });
})
return result
}
converTree(data , mapObj )
console.log(converTree(data , mapObj ))