Skip to content

Instantly share code, notes, and snippets.

@reusee
Created September 25, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reusee/a92f2f4f116ee4e742392460b9542558 to your computer and use it in GitHub Desktop.
Save reusee/a92f2f4f116ee4e742392460b9542558 to your computer and use it in GitHub Desktop.
js
let infos = [
{
"MENU_ID": 1,
"MENU_NAME": "系统管理",
"MENU_URL": "#",
"PARENT_ID": 0,//顶级菜单
"MENU_ORDER": 1//菜单的顺序
},
{
"MENU_ID": 2,
"MENU_NAME": "权限管理",
"MENU_URL": "#",
"PARENT_ID": 1,//父级菜单是系统管理
"MENU_ORDER": 1//菜单的顺序
},
];
let dict = {}
infos.forEach(info => {
dict[info.MENU_ID] = info;
});
function gen(id) {
let subs = [];
for (let menu_id in dict) {
let info = dict[menu_id];
if (info.PARENT_ID == id) {
subs.push(gen(menu_id));
delete dict[menu_id];
}
}
subs.sort((a, b) => {
return a.order > b.order;
});
if (id == 0) {
return {
id: 0,
name: 'ROOT',
subs: subs,
};
}
let info = dict[id];
let menu = {
id: id,
name: info.MENU_NAME,
url: info.MENU_URL,
subs: subs,
order: info.MENU_ORDER,
};
return menu;
}
let root = gen(0);
console.log(root);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment