遞歸實現json菜單樹過濾,過濾到匹配節點並保留其父節點javascript
//遞歸過濾節點 function nodeFilter(menuList, fiterName) { var reg = new RegExp(fiterName); var retMenuList = new Array(); if (menuList && menuList.length > 0) { for (var c of menuList) { if (c.subModules) { c.subModules = nodeFilter(c.subModules, fiterName); } if (c.thirdModules) { c.thirdModules = nodeFilter(c.thirdModules, fiterName); } if ((c.subModules && c.subModules.length > 0) || (c.thirdModules && c.thirdModules.length > 0) || isMatch(c, reg)) { retMenuList.push(c); } } } return retMenuList; } function isMatch(menu, reg) { return menu.heading.match(reg) || pinyin.getFullChars(menu.heading).toLowerCase().match(reg) || pinyin.getCamelChars(menu.heading).toLowerCase().match(reg); }