Created
May 2, 2019 02:52
-
-
Save aonrobot/efd100efa576411e2fa8311a7c38ef2f to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/yoyeji
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var menus = [ | |
{ name: "a", parent: null, seq: 1}, | |
{ name: "b", parent: "a", seq: 1}, | |
{ name: "c", parent: "a", seq: 2}, | |
{ name: "d", parent: "c", seq: 2}, | |
{ name: "e", parent: "c", seq: 1}, | |
{ name: "f", parent: "g", seq: 1}, | |
{ name: "g", parent: null, seq: 3}, | |
{ name: "h", parent: null, seq: 2}, | |
] | |
let printMenu = (menus, parent = null) => { | |
var node = [] | |
menus.filter(m => m.parent == parent).forEach(menu => { | |
node.push(Object.assign({}, menu, { child: printMenu(menus, menu.name) })) | |
}) | |
return node.sort((a,b) => a.seq - b.seq) | |
} | |
let x = printMenu(menus) | |
console.log(x) | |
// let countNum = (num) => { | |
// console.log(num) | |
// if (num <= 0) return 0 | |
// else countNum(num - 1) | |
// } | |
// countNum(10) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var menus = [ | |
{ name: "a", parent: null, seq: 1}, | |
{ name: "b", parent: "a", seq: 1}, | |
{ name: "c", parent: "a", seq: 2}, | |
{ name: "d", parent: "c", seq: 2}, | |
{ name: "e", parent: "c", seq: 1}, | |
{ name: "f", parent: "g", seq: 1}, | |
{ name: "g", parent: null, seq: 3}, | |
{ name: "h", parent: null, seq: 2}, | |
] | |
let printMenu = (menus, parent = null) => { | |
var node = [] | |
menus.filter(m => m.parent == parent).forEach(menu => { | |
node.push(Object.assign({}, menu, { child: printMenu(menus, menu.name) })) | |
}) | |
return node.sort((a,b) => a.seq - b.seq) | |
} | |
let x = printMenu(menus) | |
console.log(x) | |
// let countNum = (num) => { | |
// console.log(num) | |
// if (num <= 0) return 0 | |
// else countNum(num - 1) | |
// } | |
// countNum(10)</script></body> | |
</html> |
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
var menus = [ | |
{ name: "a", parent: null, seq: 1}, | |
{ name: "b", parent: "a", seq: 1}, | |
{ name: "c", parent: "a", seq: 2}, | |
{ name: "d", parent: "c", seq: 2}, | |
{ name: "e", parent: "c", seq: 1}, | |
{ name: "f", parent: "g", seq: 1}, | |
{ name: "g", parent: null, seq: 3}, | |
{ name: "h", parent: null, seq: 2}, | |
] | |
let printMenu = (menus, parent = null) => { | |
var node = [] | |
menus.filter(m => m.parent == parent).forEach(menu => { | |
node.push(Object.assign({}, menu, { child: printMenu(menus, menu.name) })) | |
}) | |
return node.sort((a,b) => a.seq - b.seq) | |
} | |
let x = printMenu(menus) | |
console.log(x) | |
// let countNum = (num) => { | |
// console.log(num) | |
// if (num <= 0) return 0 | |
// else countNum(num - 1) | |
// } | |
// countNum(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment