Skip to content

Instantly share code, notes, and snippets.

@robinjl
Created July 17, 2020 01:09
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 robinjl/16fc84e94be1153120e13c2a2006ef1d to your computer and use it in GitHub Desktop.
Save robinjl/16fc84e94be1153120e13c2a2006ef1d to your computer and use it in GitHub Desktop.
递归
// 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