js无限极分类

以下贴下主要代码:

var data = [
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 0, id: 1, link: "courseManger", menu_order: 1, text: "课程管理", type: "group_parent" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 1, id: 2, link: "course", menu_order: 1, text: "课程列表", type: "group_node" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 1, id: 3, link: "new", menu_order: 1, text: "新建课程", type: "group_node" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 4, id: 5, link: "order", menu_order: 1, text: "订单列表", type: "group_node" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 4, id: 6, link: "schoolManger", menu_order: 1, text: "奖学金管理", type: "group_parent" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 6, id: 7, link: "toaudit", menu_order: 1, text: "待审核列表", type: "group_node" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 6, id: 8, link: "notthrough", menu_order: 1, text: "审核未通过列表", type: "group_node" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 6, id: 9, link: "stayout", menu_order: 1, text: "待发放列表", type: "group_node" },
            { css: "el-icon-document", group_node_order: 0, group_parent_id: 0, id: 4, link: "tradManger", menu_order: 1, text: "交易管理", type: "group_parent" },
        ]

        function getData(id, arry) {
            var childArry = getParentArry(id, arry);
            if (childArry.length > 0) {
                for (var i in childArry) {
                    var sub = getData(childArry[i].id, arry);
                    if (sub.length > 0) {
                        childArry[i].sub = getData(childArry[i].id, arry);
                    } else {
                        continue;
                    }
                }
            }
            return childArry;
        }
        function getParentArry(id, arry) {
            var newArry = new Array();
            for (var i in arry) {
                if (arry[i].group_parent_id == id)
                    newArry.push(arry[i]);
            }
            return newArry;
        }

        console.log(getData(0, data));

你可能感兴趣的:(前端)