把 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;
}
相关推荐
moshuying1 天前
别让AI焦虑,偷走你本该有的底气
前端·人工智能
GIS之路1 天前
ArcPy,一个基于 Python 的 GIS 开发库简介
前端
可夫小子1 天前
OpenClaw基础-为什么会有两个端口
前端
喝拿铁写前端1 天前
Dify 构建 FE 工作流:前端团队可复用 AI 工作流实战
前端·人工智能
喝咖啡的女孩2 天前
React 合成事件系统
前端
从文处安2 天前
「九九八十一难」组合式函数到底有什么用?
前端·vue.js
前端Hardy2 天前
面试官:JS数组的常用方法有哪些?这篇总结让你面试稳了!
javascript·面试
用户5962585736062 天前
戴上AI眼镜逛花市——感受不一样的体验
前端
yuki_uix2 天前
Props、Context、EventBus、状态管理:组件通信方案选择指南
前端·javascript·react.js
老板我改不动了2 天前
前端面试复习指南【代码演示多多版】之——HTML
前端