扁平化
// 树形扁平化
export const treeDataToPlanishData = (list, parent, level = 0, clues = []) => { // eslint-disable-line
return list.reduce((prev, {
children = [],
...rest
}) => {
const parentKey = parent || '';
const keyClues = clues.concat(rest.key);
return prev.concat({ ...rest,
parentKey,
level,
isLeaf: !!parentKey,
keyClues,
}, treeDataToPlanishData(children, rest.key, level + 1, keyClues));
}, []);
};
转树形
export const planishDataToTreeData = (data) => {
const idMap = {};
const jsonTree = [];
data.forEach((v) => {
idMap[v.key] = v;
});
data.forEach((v) => {
const parent = idMap[v.parentKey];
if (parent) {
parent.children = parent.children || [];
parent.children.push(v);
} else {
jsonTree.push(v);
}
});
return jsonTree;
};