Created
September 25, 2016 19:38
-
-
Save reusee/a92f2f4f116ee4e742392460b9542558 to your computer and use it in GitHub Desktop.
js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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