把 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;
}
相关推荐
lyyl啊辉1 分钟前
5. pinia集中状态存储
vue.js
锅包一切2 分钟前
【蓝桥杯JavaScript基础入门】二、JavaScript关键特性
开发语言·前端·javascript·学习·蓝桥杯
明月_清风10 分钟前
源码回溯的艺术:SourceMap 底层 VLQ 编码与离线解析架构实战
前端·监控
明月_清风11 分钟前
WebMCP 实战指南:让你的网站瞬间变成 AI 的“大脑外挂”
前端·mcp
我不吃饼干9 小时前
TypeScript 类型体操练习笔记(二)
前端·typescript
光影少年9 小时前
浏览器渲染原理?
前端·javascript·前端框架
小白探索世界欧耶!~9 小时前
Vue2项目引入sortablejs实现表格行拖曳排序
前端·javascript·vue.js·经验分享·elementui·html·echarts
叫我一声阿雷吧10 小时前
JS实现响应式导航栏(移动端汉堡菜单)|适配多端+无缝交互【附完整源码】
开发语言·javascript·交互
GISer_Jing11 小时前
前端营销(AIGC II)
前端·react.js·aigc
NEXT0611 小时前
深度解析 JWT:从 RFC 原理到 NestJS 实战与架构权衡
前端·typescript·nestjs