把 el-cascader 的 options 平铺为一维数组(只要叶子节点)

问题

typescript 如何 把 类型为 type ElCascaderOptionsType = {

label: string;

value: string;

children?: ElCascaderOptionsType[]

} 的数组铺平的 一维字符串数组,children 之间用 ' / ' 分隔

大瓦特版本

TypeScript 复制代码
type ElCascaderOptionsType = {
    label: string;
    value: string;
    children?: ElCascaderOptionsType[]
} 


createZdStringArray(options: ElCascaderOptionsType[]): string[] {
        const result: string[] = [];

        const traverse = (node: ElCascaderOptionsType, currentPath: string[]) => {
            const newPath = [...currentPath, node.label];
            
            if (!node.children || !node.children.length) {
                result.push(newPath.join(' / '));
                return;
            }

            node.children.forEach(child => {
                traverse(child, newPath);
            });
        };

        options.forEach(option => {
            traverse(option, []);
        });

        return result;
}

deepseek 版本

TypeScript 复制代码
type ElCascaderOptionsType = {
    label: string;
    value: string;
    children?: ElCascaderOptionsType[]
} 


function flattenLeafPaths(
  options: ElCascaderOptionsType[],
  parentPath: string = ''
): string[] {
  let result: string[] = [];
  
  for (const option of options) {
    const currentPath = parentPath 
      ? `${parentPath} / ${option.label}` 
      : option.label;
    
    if (!option.children || option.children.length === 0) {
      // 叶子节点
      result.push(currentPath);
    } else {
      // 非叶子节点,继续递归
      const childPaths = flattenLeafPaths(option.children, currentPath);
      result = result.concat(childPaths);
    }
  }
  
  return result;
} 

deepseek 给出大瓦特的优化版本

TypeScript 复制代码
type ElCascaderOptionsType = {
    label: string;
    value: string;
    children?: ElCascaderOptionsType[]
}


function flattenLeafPaths(
  options: ElCascaderOptionsType[],
  parentPath: string[] = []
): string[] {
  const result: string[] = [];
  
  for (const option of options) {
    const currentPath = [...parentPath, option.label];
    
    if (!option.children || option.children.length === 0) {
      result.push(currentPath.join(' / '));
    } else {
      result.push(...flattenLeafPaths(option.children, currentPath));
    }
  }
  
  return result;
}

豆包给出大瓦特的优化版本

TypeScript 复制代码
// 定义类型(方便理解,实际项目中已有 ElCascaderOptionsType)
type ElCascaderOptionsType = {
  label: string;
  value?: string | number;
  children?: ElCascaderOptionsType[];
};

function createZdStringArray(options: ElCascaderOptionsType[]): string[] {
  // 空输入直接返回空数组,避免无效遍历
  if (!Array.isArray(options) || options.length === 0) {
    return [];
  }

  const result: string[] = [];

  // 递归遍历函数,职责单一:处理单个节点的路径
  const traverse = (node: ElCascaderOptionsType, currentPath: string[]) => {
    // 过滤空 label,避免路径出现 " /  / " 等异常
    const validLabel = node.label?.trim() || '';
    if (!validLabel) return;

    // 浅拷贝路径数组,避免修改父级路径(核心:保持路径的独立性)
    const newPath = [...currentPath, validLabel];

    // 叶子节点:拼接路径并加入结果
    if (!node.children || !node.children.length) {
      result.push(newPath.join(' / '));
      return;
    }

    // 非叶子节点:递归遍历子节点
    node.children.forEach(child => traverse(child, newPath));
  };

  // 遍历根节点,初始化路径为空数组
  options.forEach(option => traverse(option, []));

  return result;
}
相关推荐
用户0595401744615 小时前
把 Redis 持久化测试从 800 行 Shell 换成 30 行 pytest,排错效率翻了 10 倍
前端·css
Rkgua15 小时前
事件流模型是什么和DOM事件模型等关系
javascript
GISer_Jing15 小时前
AI全栈工程师知识体系全景:从前后端核心架构到落地项目全拆解
前端·人工智能·后端·ai编程
W.A委员会15 小时前
多行溢出在末尾添加省略号
开发语言·javascript·css
Wect15 小时前
深度剖析浏览器跨域问题
前端·面试·浏览器
陈随易15 小时前
bun将会支持Bun.image,你怎么看?
前端·后端·程序员
jingqingdai316 小时前
别用正则格式化 HTML!我用 DOM 遍历实现零风险本地格式化,老项目重构效率直接拉满
前端·重构·html
木斯佳16 小时前
前端八股文面经大全:腾讯前端实习二、三OC面(2026-04-27)·面经深度解析
前端·状态模式
Python私教16 小时前
如意Agent日志系统重构:从 print() 大海捞针到结构化可观测性栈
java·前端·重构