把 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;
}
相关推荐
lendsomething2 小时前
graalvm使用实战:在java中执行js脚本
java·开发语言·javascript·graalvm
Kiyra2 小时前
阅读 Netty 源码关于 NioEventLoop 和 Channel 初始化部分的思考
运维·服务器·前端
冰暮流星2 小时前
javascript的switch语句介绍
java·前端·javascript
小简GoGo3 小时前
前端常用设计模式快速入门
javascript·设计模式
做科研的周师兄3 小时前
【MATLAB 实战】|多波段栅格数据提取部分波段均值——批量处理(NoData 修正 + 地理信息保真)_后附完整代码
前端·算法·机器学习·matlab·均值算法·分类·数据挖掘
da_vinci_x3 小时前
图标量产:从“手绘地狱”到“风格克隆”?Style Reference 的工业化实战
前端·游戏·ui·prompt·aigc·设计师·游戏美术
利刃大大3 小时前
【ES6】变量与常量 && 模板字符串 && 对象 && 解构赋值 && 箭头函数 && 数组 && 扩展运算符 && Promise/Await/Async
开发语言·前端·javascript·es6
天若有情6733 小时前
ES6 模块与 CommonJS 的区别详解
前端·javascript·es6
大猫会长3 小时前
postgreSQL中,RLS的using与with check
开发语言·前端·javascript