调用函数时传递的值
let resultArr = getAllList([ { name: '一级路由', path: 'first/', component: 'first', children: [ { name: '二级路由', path: 'first/child', component: 'firstchild', children: [ { name: '三级路由', path: 'first/child/child', component: 'firstchildchild', }, ], }, { name: '二级路由', path: 'first/child', component: 'firstchild', children: [ { name: '三级路由', path: 'first/child/child', component: 'firstchildchild', }, ], }, { name: '二级路由', path: 'first/child', component: 'firstchild', children: [ { name: '三级路由', path: 'first/child/child', component: 'firstchildchild', }, ], }, ], }, { name: '二级路由', path: 'second/', component: 'second', children: [ { name: '二级路由', path: 'two/child', component: 'towchild', children: [ { name: '三级路由', path: 'first/child/child', component: 'firstchildchild', }, ], }, ], }, ]) console.log(resultArr, '这里是resultArr')
执行过程:如果一直由children 那么就一直调用自身,直到最后一层则返回对应的值
const getAllList = (currentArr: any) => { let result = currentArr.map((item: any) => { if (item.children && item.children.length && item.children[0]) { item.children = getAllList(item.children) return { name: item.name, path: item.path, component: item.component, children: item.children, role: '超人', } } else { return { name: item.name, path: item.path, component: item.component, } } }) return result }
这里是返回值