如何用递归函数动态解析多层级分类数组(解决层级限制问题)
解决分类数组的递归树形解析问题
我明白你现在的困扰——手里这个包含分类、子分类和总计项的数组,原有的递归代码只能处理两层结构,没法适配更深的层级。咱们来改造代码,实现你想要的多层级树形结构。
先分析原代码的问题
你的原代码有几个关键问题导致无法处理多层结构:
- 依赖全局变量
parsedArray,递归时会覆盖之前的分类数据,没法形成嵌套关系 - 递归调用
parseArray后,没有把返回的子结构挂载到父分类节点上 - 没有明确区分叶子节点(没有对应Total项的条目)和父分类节点(有对应Total项的条目)
改进后的实现方案
我们重新设计递归逻辑:让递归函数返回当前分类的完整结构,先建立分类与对应Total项的映射,方便快速判断是否为父分类,再递归嵌套子结构。
首先模拟getLineItems函数(因为你没给出具体实现,这里模拟它获取start到end之间的所有条目):
// 模拟getLineItems函数:获取从start到end之间的所有条目(不包含start和end本身) function getLineItems(start, end, fullItems) { const startIndex = fullItems.indexOf(start); const endIndex = fullItems.indexOf(end); if (startIndex === -1 || endIndex === -1 || startIndex >= endIndex) { return []; } return fullItems.slice(startIndex + 1, endIndex); }
然后是核心的递归解析函数,它会返回当前分类的树形结构:
function parseCategoryItems(fullItems, currentCategory) { // 先检查当前分类是否有对应的Total项 const totalKey = `Total ${currentCategory}`; const hasChildren = fullItems.includes(totalKey); if (!hasChildren) { // 没有子分类,作为叶子节点返回 return { name: currentCategory }; } // 获取当前分类下的所有子条目 const childItems = getLineItems(currentCategory, totalKey, fullItems); // 递归处理每个子条目,构建子节点数组 const children = childItems.map(item => parseCategoryItems(fullItems, item)); // 返回父分类节点,包含子节点 return { name: currentCategory, children: children }; } // 初始化解析整个数组的入口函数 function parseFullArray(fullItems) { // 顶层条目是那些没有被任何Total项包含的条目 // 先收集所有Total项对应的父分类,排除这些父分类的父级 const totalItems = fullItems.filter(item => item.startsWith('Total ')); const childCategories = totalItems.map(total => total.replace('Total ', '')); // 顶层条目是不在childCategories里的(因为它们没有父分类) const topLevelItems = fullItems.filter(item => !childCategories.includes(item) && !item.startsWith('Total ')); // 递归处理每个顶层条目 return topLevelItems.map(item => parseCategoryItems(fullItems, item)); }
调用示例
用你提供的数组来测试:
const items = [ "SuSPENSE", "Subcontractor Expense", "Data Entry", "Design", "Programming", "Subcontractor Expense - Other", "Total Subcontractor Expense", "Technology-Communication", "Licenses", "Domain Hosting Fee", "Internet", "Internet Servers", "Internet - Other", "Total Internet", "Telephone", "Call Center", "Cellular Phones", "Fax", "Telephone - Other", "Total Telephone", "Hardware/Software", "Computer Software", "Hardware/Software - Other", "Total Hardware/Software", "Technology-Communication - Other", "Total Technology-Communication" ]; const result = parseFullArray(items); console.log(JSON.stringify(result, null, 2));
输出的树形结构示例
输出的结构会是这样的(简化版):
[ { "name": "SuSPENSE" }, { "name": "Subcontractor Expense", "children": [ { "name": "Data Entry" }, { "name": "Design" }, { "name": "Programming" }, { "name": "Subcontractor Expense - Other" } ] }, { "name": "Technology-Communication", "children": [ { "name": "Licenses" }, { "name": "Domain Hosting Fee" }, { "name": "Internet", "children": [ { "name": "Internet Servers" }, { "name": "Internet - Other" } ] }, { "name": "Telephone", "children": [ { "name": "Call Center" }, { "name": "Cellular Phones" }, { "name": "Fax" }, { "name": "Telephone - Other" } ] }, { "name": "Hardware/Software", "children": [ { "name": "Computer Software" }, { "name": "Hardware/Software - Other" } ] }, { "name": "Technology-Communication - Other" } ] } ]
这个方案可以处理任意层级的嵌套结构,不管你的分类有多少层,递归都会自动遍历处理。
内容的提问来源于stack exchange,提问作者Vaxo Basilidze
相关产品推荐
相关产品推荐

