// mockData
var data = [
{"id": "1", "name": "a", "pid": "0"},
{"id": "1.1", "name": "a1", "pid": "1"},
{"id": "1.2", "name": "a2", "pid": "1"},
{"id": "2", "name": "b", "pid": "0"},
{"id": "2.1", "name": "b1", "pid": "2"},
{"id": "2.2", "name": "b2", "pid": "2"}
];
生成经典 tree 数据
function createTree(id, data) {
let tree = []
data.forEach((item, i) => {
if(id == item.pid) {
tree.push({
label: item.name,
children: createTree(item.id, data)
})
}
})
return tree
}
console.log(createTree(0, data))
网友评论