Skip to content

Instantly share code, notes, and snippets.

@Teemwu
Last active April 27, 2022 01:01
Show Gist options
  • Save Teemwu/68ee512fb89f00170aa2908e5a693ff5 to your computer and use it in GitHub Desktop.
Save Teemwu/68ee512fb89f00170aa2908e5a693ff5 to your computer and use it in GitHub Desktop.
获取菜单目录路径
{
"scripts": [],
"showConsole": true
}
/**
* 获取菜单目录路径
* 例:项目中的面包屑生成
*/
const menuTree = [
{
title: '1',
path: 'path',
children: [
{ title: '1.1', path: 'path/1' },
{
title: '1.2',
path: 'path/2',
children: [
{ title: '1.2.3', path: 'path/2/1' },
{ title: '1.2.3', path: 'path/2/2' },
{ title: '1.2.3', path: 'path/2/3' },
]
},
{ title: '1.3', path: 'path/3' },
]
}
]
const currentPath = 'path/2/2'
function init () {
let found = false
const result = []
const search = (tree) => {
if (!tree) return
for (let i = 0;i < tree.length;i++) {
if (found) return
const item = tree[i]
result.push(item)
if (item.path === currentPath) found = true
search(item.children)
if (!found) result.pop()
}
}
search(menuTree)
console.log(result.map(i => i.title))
}
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment