扁平化树结构数据

// 扁平化当前数据
export function flattenList(nodes, parentPath = []) {
  const list = [];
  nodes.forEach((node, index) => {
    const currentPath = [...parentPath, index + 1];
    const flatNode = { ...node };
    list.push(flatNode);
    // 递归处理子节点并合并结果
    if (flatNode.children && flatNode.children.length) {
      const childNodes = flattenList(flatNode.children, currentPath);
      list.push(...childNodes);
    }
  });
  return list;
}

你可能感兴趣的:(扁平化树结构数据)