javascript - 通过嵌套元素进行映射时,递归函数无法按预期工作
问题描述
我试图通过一个对象数组递归映射,这些对象可能嵌套了相同类型的对象。
IE,
type TOption = {
id: string;
name: string;
options?: TOption;
}
const options = [
{ id: "1", name: "Option 1" },
{
id: "2",
name: "Option 2",
options: [
{
id: "3",
name: "Option 3",
options: [
{
id: "4",
name: "Option 4"
}
]
}
]
},
{ id: "5", name: "Option 5" }
] as TOption[];
有点前提,我正在尝试构建一个导航栏菜单,其中每个类型的元素TOption
也可以有一个options
类型字段的子菜单TOption
。基于level
,我将在 div 之外/屏幕外偏移该嵌套菜单。
无论如何,当我遍历数组时遇到了一些麻烦。这是我的 JS 化recurse
方法(实际React.useCallback
功能可以在下面的沙箱中看到),
const recurse = React.useCallback(
({ options, level }) =>
options.map(({ id, name, options: subOptions = [] }) =>
subOptions.length ? (
recurse({ options: subOptions, level: level + 1 })
) : (
console.log(`My id is {id} and I'm at level {level}`)
)
),
[]
);
以上输出,
My id is 1 and I'm at level 1
My id is 4 and I'm at level 3
My id is 5 and I'm at level 1
它显然只进入具有子菜单的元素,并且递归以没有子菜单的菜单结束。
我怎样才能改变它,使输出变成,
My id is 1 and I'm at level 1
My id is 2 and I'm at level 1
My id is 3 and I'm at level 2
My id is 4 and I'm at level 3
My id is 5 and I'm at level 1
// Where those at level 1 would be displayed in the "parent" menu,
// and those at level 2 would be a secondary submenu of that parent menu,
// and those at level 3 would a submenu of a secondary menu.
3 级菜单的示例是Settings -> Layout -> Theme
,其结构是,
const options = [
...restOfOptions
{
id: "2",
name: "Settings", // level 1
options: [
{
id: "3",
name: "Layout", // level 2
options: [
{
id: "4",
name: "Theme", // level 3
}
]
}
]
},
];
这里的问题是,如果存在子菜单,则该函数将返回该树中存在的最后一个子菜单。
在沙箱中,输出是,
My id is 1 and I'm at level 1
My id is 4 and I'm at level 3
My id is 6 and I'm at level 4
My id is 7 and I'm at level 1
但我正在寻找,
My id is 1 and I'm at level 1
My id is 2 and I'm at level 1
My id is 3 and I'm at level 2
My id is 4 and I'm at level 3
My id is 5 and I'm at level 3
My id is 6 and I'm at level 4
My id is 7 and I'm at level 7
解决方案
在“subOptions.length”控件中,如果有子选项,则不会写入当前数据,因为使用了递归。
实际上,需要先写入当前数据,如果有子选项,再调用。
推荐阅读
- javascript - 引导程序
- python - 有谁知道为什么我在尝试使用 pip 时会出错?我刚刚下载了最新版本的python,但它无法正常工作
- sql-server - SnowFlake 函数不返回数据和错误
- visual-studio - 如何修复为 Stimulsoft.Reports.Engine.NetCore 检测到的 NuGet 错误版本冲突
- database - 如何打开oracle数据库?
- python - 根据条件连接熊猫中的行
- tensorflow - 关于 Tensorflow 扩展 BigQuery 作为 ExampleGen 的问题
- python - Airflow PythonVirtualOperator 用简单的例子抛出错误
- amazon-web-services - Route 53 配置 S3 url
- react-native - 我想使用在渲染之前初始化的组件生命周期方法