const buildTree = (newdata, parentId) => {
const tree = [];
// 遍历数据
newdata.forEach((item) => {
// 如果当前节点的 parentId 与父节点的 id 相等,说明是父节点的子节点
if (item.parent === parentId) {
// 使用递归调用,将子节点也转换为树形结构
const children = buildTree(newdata, item.id);
// 如果子节点存在,则将子节点添加到当前节点的 children 属性中
if (children.length > 0) {
item.children = children;
}
// 将当前节点添加到树中
tree.push(item);
}
});
return tree;
};
示例:
tagstype.value = buildTree(newtagList, null); // 假设父节点的字段值为null