zoukankan      html  css  js  c++  java
  • 根据子节点匹配出树形结构的路径

     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         }
  • 相关阅读:
    基本排序算法汇总
    贪心算法题目汇总
    STL中sort
    栈和队列题目汇总
    cron 计划任务 在线配置
    各种less概念通俗解释
    node 子线程 进程
    内存池
    RPC简介
    koa express 对比
  • 原文地址:https://www.cnblogs.com/chenzeyongjsj/p/11888321.html
Copyright © 2011-2022 走看看