记录共通的js方法
父子关系的数组转换成树形结构数据:
translateDataToTree('parentUuid', 'uuid', datas)
export function translateDataToTree (field, curKey, data) {
const parentMap = {}
const tree = []
data.forEach((item) => {
if (!item[field]) {
parentMap[item[curKey]] = { ...item, children: [] }
tree.push(parentMap[item[curKey]])
}
})
data.forEach((item) => {
if (item[field] && parentMap[item[field]]) {
parentMap[item[field]].children.push({ ...item, children: [] })
}
})
return tree
}
树形结构数据转换成父子关系的数组:
translateTreeToData([data])
export function translateTreeToData(data) {
const result = [];
const loop = (node) => {
result.push(node);
const children = node.children;
if (children) {
children.forEach((child) => {
loop(child);
});
}
};
data.forEach((item) => {
loop(item);
});
return result;
}