递归遍历多层次菜单

Posted 熬夜的小青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归遍历多层次菜单相关的知识,希望对你有一定的参考价值。

//根据后台数据格式化菜单
function formatMenu(data) 
    return data.map(function (item) 
        if (!item) 
            return null;
        
        var path = item.path, name = item.menuName, id = item.id;

        var result = 
            path: path,
            name: name,
            id: id
        ;

        if (item.pid > 0) 
            result.exact = true;
        
        if (item.ch) 
            var children = formatMenu(item.ch);
            result.children = children;
        
        return result;
    ).filter(function (item) 
        return item
    );

 

 

以上是关于递归遍历多层次菜单的主要内容,如果未能解决你的问题,请参考以下文章