js 树形结构问题处理?
将如下结构中的type=1的内容提取到父级。层级可能是无限极。
const routes = [
{
name: "首页",
type: 0,
children: [],
},
{
name: "系统管理",
type: 0,
children: [
{
name: "用户管理",
type: 0,
children: [
{
name: "新建用户",
type: 1,
children: [],
},
],
},
{
name: "角色管理",
type: 0,
children: [
{
name: "新建角色",
type: 1,
children: [],
},
{
name: "成员管理",
type: 1,
children: [],
},
],
},
],
},
];
期望:
const routes = [
{
name: "首页",
type: 0,
children: [],
},
{
name: "系统管理",
type: 0,
children: [
{
name: "用户管理",
type: 0,
children: [],
},
{
name: "新建用户",
type: 1,
children: [],
},
{
name: "角色管理",
type: 0,
children: [],
},
{
name: "新建角色",
type: 1,
children: [],
},
{
name: "成员管理",
type: 1,
children: [],
},
],
},
];
1 回复
- 转换成字符串, 用正则匹配出来
- 用递归
可惜这里空白的地方太小,我写不下