本文档详细介绍了如何使用JavaScript从深度嵌套的分类数据中提取特定ID的子节点,并将结果扁平化为一个数组。我们提供了一个高效的算法,避免了传统的循环结构,而是采用栈数据结构和条件判断,以实现目标。同时,我们还讨论了在没有指定ID时返回父节点和直接子节点,以及在指定ID没有子节点时返回空数组的策略。
在处理具有层级结构的分类数据时,经常需要根据特定的分类ID提取其所有子节点,并将这些节点扁平化到一个数组中。本教程将介绍一种使用JavaScript实现此功能的有效方法,该方法避免了使用for、forEach和while等传统循环结构,而是采用栈数据结构,提供了一种更简洁和可读性更高的解决方案。
首先,我们假设有如下的数据结构,表示分类信息:
interface Category { name: string; id: string; count: string; depth: string; children: Category[]; } const data: Category[] = [ { name: "Car", id: "19", count: "20", depth: "1", children: [ { name: "Wheel", id: "22", count: "3", depth: "2", children: [ { name: "Engine", id: "101", count: "1", depth: "3", children: [ { name: "Engine and Brakes", id: "344", count: "1", depth: "4", children: [] } ] } ] } ] }, { name: "Bike", id: "3", count: "12", depth: "1", children: [ { name: "SpeedBike", id: "4", count: "12", depth: "2", children: [] } ] } ];
以下是实现提取指定ID子节点并扁平化的函数:
立即学习“Java免费学习笔记(深入)”;
interface Category { name: string; id: string; count: string; depth: string; children: Category[]; } const mapCategory = (category: Category) => ({ name: category.name, id: category.id, count: category.count, }); const getCategoriesChildren = ( categoryIds: Category['id'][], categories: Category[], ) => { const foundChildren: Pick<Category, 'id' | 'count' | 'name'>[] = []; if (categoryIds.length === 0) { return categories.reduce<Pick<Category, 'id' | 'count' | 'name'>[]>( (acc, category) => { acc.push(mapCategory(category), ...category.children.map(mapCategory)); return acc; }, [], ); } const stack: (Category & { isDesired?: boolean })[] = [...categories]; while (stack.length) { const category = stack.pop(); if (!category) continue; const isDesiredCategory = categoryIds.includes(category.id) || category.isDesired; if (isDesiredCategory) { foundChildren.push(...category.children.map(mapCategory)); } stack.push( ...(isDesiredCategory ? category.children.map((child) => ({ ...child, isDesired: true })) : category.children), ); } return foundChildren; };
代码解释:
const categoryIds1 = ['22', '3']; const result1 = getCategoriesChildren(categoryIds1, data); console.log(result1); // Expected Output: // [ // { name: "Engine", id: "101", count: "1" }, // { name: "Engine and Brakes", id: "344", count: "1" }, // { name: "SpeedBike", id: "4", count: "12" } // ] const categoryIds2: string[] = []; const result2 = getCategoriesChildren(categoryIds2, data); console.log(result2); // Expected Output: // [ // { name: "Car", id: "19", count: "20" }, // { name: "Wheel", id: "22", count: "3" }, // { name: "Bike", id: "3", count: "12" }, // { name: "SpeedBike", id: "4", count: "12" } // ] const categoryIds3 = ['4']; const result3 = getCategoriesChildren(categoryIds3, data); console.log(result3); // Expected Output: // []
本教程介绍了一种使用JavaScript从深度嵌套的分类数据中提取特定ID的子节点,并将结果扁平化为一个数组的有效方法。该方法避免了传统的循环结构,而是采用栈数据结构,提供了一种更简洁和可读性更高的解决方案。 通过理解和应用本教程中的代码,你可以更有效地处理具有层级结构的分类数据,并提取所需的信息。
以上就是从嵌套的分类数据中提取指定ID的子节点并扁平化:JavaScript教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号