我有一个像bellow一样的数据文件:
const website_settings = {
// 网站的设置的 menu 的数据
website_settings_menu_data: [
{
"name":"网站首页设置",
"icon":"settings",
"groups": [
{
"name": "网站首页",
"icon": "settings",
"children": [
.....
{
"name": "地图导航",
"route": "" // 跳转路径
},
{
"name": "页脚导航",
"route": "" // 跳转路径
}
]
}
]
},
{
"name":"网站新闻页设置",
"icon":"settings",
"groups": [
{
"name": "网站新闻页设置",
"icon": "settings",
"children": [
{
"name": "网站新闻页设置",
"route": "" // 跳转路径
}
......
]
}
]
},
{
"name":"实体服务器页面设置",
"icon":"settings",
"groups": [
{
"name": "实体服务器页面设置",
"icon": "settings",
"children": [
{
"name": "实体服务器页面设置",
"route": "" // 跳转路径
}
]
}
]
},
{
"name":"通知公告设置",
"icon":"settings",
"groups": [
{
"name": "公告设置",
"icon": "settings",
"children": [
{
"name": "notice-settings",
"route": "abc" // 跳转路径
}
]
}
]
}
]
}
export default website_settings;
要求提供name
,例如notice-settings
,我想查询相关的route
,在示例中应该有abc
(最后一个)。
在我的想法中,我可以使用双for循环来查询匹配的name
,但我不确定是否有更好的存档方法,你能看一下吗?
答案 0 :(得分:1)
你可以建立一次Map,所以每次查找都是O(1):
nodeInfo-..
所以现在它很简单:
const routes = new Map;
function check(array) {
for(const { name, groups, children, route } of array) {
if(children) check(children);
if(groups) check(groups);
if(name && route) routes.set(name, route);
}
}
check(website_settings_menu_data)