Created
July 17, 2020 01:09
-
-
Save robinjl/16fc84e94be1153120e13c2a2006ef1d to your computer and use it in GitHub Desktop.
递归
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
// 1*2*3*...100 | |
function recrusive(num){ | |
if(num === 1){ | |
return num; | |
}else{ | |
return num*recrusive(num-1) | |
} | |
} | |
// 菜单递归 后端返回递归结构,前端更换键名 例如 url -> path | |
function menuRecursive(data) { | |
return data.map(item => { | |
const { url, children, ...rest } = item; | |
const result = { | |
path: url, | |
...rest | |
} | |
if (children) { | |
result.children = menuRecursive(children) | |
} | |
return result | |
}) | |
} | |
const menu = [ | |
{ | |
url: 1, | |
children: | |
[ | |
{ | |
url: 11, children: [ | |
{ url: 111 } | |
] | |
}, | |
{ | |
url: 22, children: [ | |
{ url: 222 } | |
] | |
} | |
] | |
}, | |
{ | |
url: 2, | |
children: [] | |
} | |
]; | |
console.log(recrusive(3)) // 6 | |
console.log(menuRecursive(menu)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment