首页 > 其他 > 详细

根据子节点匹配出树形结构的路径

时间:2019-11-19 11:48:30      阅读:124      评论:0      收藏:0      [点我收藏+]
 1 /* 递归匹配菜单路径 */
 2         recursionMenu: function(recursionField = ‘‘, menu = []) {
 3             let menuTree = {
 4                 title: ‘首页‘,
 5                 url: ‘/pages/index‘,
 6                 children: menu
 7             };
 8             let _menu = []; // 路径数组
 9             let findIt = false; // 是否匹配到
10             let getPath = function(tree) {
11                 _menu.push({
12                     title: tree.title,
13                     path: tree.url
14                 });
15                 if (tree.title === recursionField) {
16                     findIt = true;
17                     return;
18                 }
19                 if (tree.children && tree.children.length > 0) {
20                     for (let i = 0; i < tree.children.length; i++) {
21                         getPath(tree.children[i]);
22                         if (findIt) return;
23                     }
24                     _menu.pop();
25                 } else if (!tree.children || tree.children.length === 0) {
26                     _menu.pop();
27                 }
28             };
29             getPath(menuTree);
30             return _menu;
31         }

 

根据子节点匹配出树形结构的路径

原文:https://www.cnblogs.com/chenzeyongjsj/p/11888321.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!