//树形数据铺成数组
tree2arr(tree) {
let arr = []
const expanded = datas => {
if (datas && datas.length > 0) {
datas.forEach(e => {
arr.push(e)
expanded(e.children)
})
}
}
expanded(tree)
return arr
}
// 数组拼成树形数据
arr2tree(data) {
let result = []
if (!Array.isArray(data)) {
return result
}
data.forEach(item => {
delete item.children;
});
let map = {};
data.forEach(item => {
map[item.id] = item;
});
data.forEach(item => {
let parent = map[item.pid];
if (parent) {
(parent.children || (parent.children = [])).push(item);
} else {
result.push(item);
}
});
return result;
}