js遍歴treeの一例(全遍歴)

136963 ワード

すべて巡回する
 実測は本当に有効です.
仕事の中で出会う問題は比較的に価値のある問題と言えるべきです.
DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
head>

<body>
    <script>
        var a = [
            {
                "id": 1,
                "name": "     ",
                "childrenIds": "10,11,12,13",
                "parentId": 0,
                "orderNum": 1,
                "url": "/lightApp/light/list",
                "description": "     ",
                "createTime": "2018-09-19 17:18:25",
                "modifyTime": "2018-09-21 11:17:54",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": true,
                "childrens": [
                    {
                        "id": 10,
                        "name": "     ",
                        "childrenIds": null,
                        "parentId": 1,
                        "orderNum": 1,
                        "url": "/lightApp/light/list",
                        "description": "     ",
                        "createTime": "2018-09-19 17:25:49",
                        "modifyTime": "2018-09-19 17:25:49",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": true,
                        "childrens": null
                    },
                    {
                        "id": 11,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 1,
                        "orderNum": 2,
                        "url": "/lightApp/auditInfo/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:26:28",
                        "modifyTime": "2018-09-19 17:26:28",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 12,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 1,
                        "orderNum": 3,
                        "url": "/lightApp/auditDevelop/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:26:52",
                        "modifyTime": "2018-09-19 17:26:52",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 13,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 1,
                        "orderNum": 4,
                        "url": "/lightApp/auditInterface/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:27:12",
                        "modifyTime": "2018-09-19 17:27:12",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 2,
                "name": "     ",
                "childrenIds": "17,18,15,14,16",
                "parentId": 0,
                "orderNum": 2,
                "url": "/applet/builtIn/list",
                "description": "     ",
                "createTime": "2018-09-19 17:19:36",
                "modifyTime": "2018-09-19 17:19:36",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": true,
                "childrens": [
                    {
                        "id": 14,
                        "name": "     ",
                        "childrenIds": null,
                        "parentId": 2,
                        "orderNum": 1,
                        "url": "/applet/builtIn/list",
                        "description": "     ",
                        "createTime": "2018-09-19 17:27:55",
                        "modifyTime": "2018-09-19 17:27:55",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 15,
                        "name": "     ",
                        "childrenIds": null,
                        "parentId": 2,
                        "orderNum": 2,
                        "url": "/applet/externalSet/list",
                        "description": "     ",
                        "createTime": "2018-09-19 17:28:27",
                        "modifyTime": "2018-09-19 17:28:27",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 16,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 2,
                        "orderNum": 3,
                        "url": "/applet/auditInfo/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:29:17",
                        "modifyTime": "2018-09-19 17:29:17",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 17,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 2,
                        "orderNum": 4,
                        "url": "/applet/auditDevelop/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:29:52",
                        "modifyTime": "2018-09-19 17:29:52",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 18,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 2,
                        "orderNum": 5,
                        "url": "/applet/appletInterface/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:30:22",
                        "modifyTime": "2018-09-19 17:30:22",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 3,
                "name": "    ",
                "childrenIds": "19,20",
                "parentId": 0,
                "orderNum": 3,
                "url": "/template/message/list",
                "description": "    ",
                "createTime": "2018-09-19 17:20:14",
                "modifyTime": "2018-09-19 17:20:14",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": false,
                "childrens": [
                    {
                        "id": 19,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 3,
                        "orderNum": 1,
                        "url": "/template/message/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:31:29",
                        "modifyTime": "2018-09-19 17:31:29",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 20,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 3,
                        "orderNum": 2,
                        "url": "/template/order/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:32:01",
                        "modifyTime": "2018-09-19 17:32:01",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 4,
                "name": "    ",
                "childrenIds": "21,22,23",
                "parentId": 0,
                "orderNum": 4,
                "url": "/projects/project/list",
                "description": "    ",
                "createTime": "2018-09-19 17:20:38",
                "modifyTime": "2018-09-19 17:20:38",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": false,
                "childrens": [
                    {
                        "id": 21,
                        "name": "    ",
                        "childrenIds": null,
                        "parentId": 4,
                        "orderNum": 1,
                        "url": "/projects/project/list",
                        "description": "    ",
                        "createTime": "2018-09-19 17:32:44",
                        "modifyTime": "2018-09-19 17:32:44",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 22,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 4,
                        "orderNum": 2,
                        "url": "/projects/application/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:33:11",
                        "modifyTime": "2018-09-19 17:33:11",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 23,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 4,
                        "orderNum": 3,
                        "url": "/projects/versions/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:33:32",
                        "modifyTime": "2018-09-19 17:33:32",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 5,
                "name": "    ",
                "childrenIds": "24",
                "parentId": 0,
                "orderNum": 5,
                "url": "/information/announcement/list",
                "description": "    ",
                "createTime": "2018-09-19 17:20:47",
                "modifyTime": "2018-09-19 17:20:47",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": false,
                "childrens": [
                    {
                        "id": 24,
                        "name": "    ",
                        "childrenIds": null,
                        "parentId": 5,
                        "orderNum": 1,
                        "url": "/information/announcement/list",
                        "description": "    ",
                        "createTime": "2018-09-19 17:34:00",
                        "modifyTime": "2018-09-19 17:34:00",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 6,
                "name": "    ",
                "childrenIds": "25,26,27,28",
                "parentId": 0,
                "orderNum": 6,
                "url": "/user/registerRecord/list",
                "description": "    ",
                "createTime": "2018-09-19 17:21:02",
                "modifyTime": "2018-09-19 17:21:02",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": true,
                "childrens": [
                    {
                        "id": 25,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 6,
                        "orderNum": 1,
                        "url": "/user/registerRecord/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:34:26",
                        "modifyTime": "2018-09-19 17:34:26",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": true,
                        "childrens": null
                    },
                    {
                        "id": 26,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 6,
                        "orderNum": 2,
                        "url": "/user/certificationApplication/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:34:41",
                        "modifyTime": "2018-09-19 17:34:41",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 27,
                        "name": "       ",
                        "childrenIds": null,
                        "parentId": 6,
                        "orderNum": 3,
                        "url": "/user/developerCertification/list",
                        "description": "       ",
                        "createTime": "2018-09-19 17:35:29",
                        "modifyTime": "2018-09-19 17:35:29",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 28,
                        "name": "    ",
                        "childrenIds": null,
                        "parentId": 6,
                        "orderNum": 4,
                        "url": "/user/loginLog/list",
                        "description": "    ",
                        "createTime": "2018-09-19 17:36:03",
                        "modifyTime": "2018-09-19 17:36:03",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 7,
                "name": "    ",
                "childrenIds": "29",
                "parentId": 0,
                "orderNum": 7,
                "url": "/terminal/terminal/list",
                "description": "    ",
                "createTime": "2018-09-19 17:21:18",
                "modifyTime": "2018-09-19 17:21:18",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": false,
                "childrens": [
                    {
                        "id": 29,
                        "name": "    ",
                        "childrenIds": null,
                        "parentId": 7,
                        "orderNum": 1,
                        "url": "/terminal/terminal/list",
                        "description": "    ",
                        "createTime": "2018-09-19 17:36:34",
                        "modifyTime": "2018-09-19 17:36:34",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 8,
                "name": "    ",
                "childrenIds": "30",
                "parentId": 0,
                "orderNum": 8,
                "url": "/basics/appAttribute/list",
                "description": "    ",
                "createTime": "2018-09-19 17:21:34",
                "modifyTime": "2018-09-19 17:21:34",
                "status": "ENABLE",
                "mustUse": "NO",
                "own": false,
                "childrens": [
                    {
                        "id": 30,
                        "name": "      ",
                        "childrenIds": null,
                        "parentId": 8,
                        "orderNum": 1,
                        "url": "/basics/appAttribute/list",
                        "description": "      ",
                        "createTime": "2018-09-19 17:36:57",
                        "modifyTime": "2018-09-19 17:36:57",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    }
                ]
            },
            {
                "id": 9,
                "name": "    ",
                "childrenIds": "31,32",
                "parentId": 0,
                "orderNum": 9,
                "url": null,
                "description": "    ",
                "createTime": "2018-09-19 17:22:41",
                "modifyTime": "2018-09-19 17:22:41",
                "status": "ENABLE",
                "mustUse": "YES",
                "own": false,
                "childrens": [
                    {
                        "id": 31,
                        "name": "    ",
                        "childrenIds": null,
                        "parentId": 9,
                        "orderNum": 1,
                        "url": null,
                        "description": "    ",
                        "createTime": "2018-09-19 17:40:02",
                        "modifyTime": "2018-09-19 17:40:02",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": false,
                        "childrens": null
                    },
                    {
                        "id": 32,
                        "name": "    ",
                        "childrenIds": null,
                        "parentId": 9,
                        "orderNum": 2,
                        "url": null,
                        "description": "    ",
                        "createTime": "2018-09-19 17:40:42",
                        "modifyTime": "2018-09-19 17:40:42",
                        "status": "ENABLE",
                        "mustUse": "NO",
                        "own": true,
                        "childrens": null
                    }
                ]
            }
        ];
        var tickMenuIdFilter = (function () {
            var resultArr = new Array();
            var getTickMenuId = function (obj) {
                if (undefined == obj || null == obj || !obj instanceof Object) {
                    return;
                }
                if (obj.own) {
                    resultArr.push(obj.id);
                }
                if (null != obj.childrens && obj.childrens instanceof Array) {
                    for (let child of obj.childrens) {
                        getTickMenuId(child);
                    }
                }

            }

            return {
                filter: function (arr) {
                    if (!arr instanceof Array) {
                        return false;
                    }
                    resultArr = new Array();
                    for (let rootMenu of arr) {
                        getTickMenuId(rootMenu);
                    }
                    return resultArr;
                }
            }
        })();
        var b = tickMenuIdFilter.filter(a);
        console.log("a", a);
        console.log("b", b); //  :1,10,2,32
    script>
body>

html>
 
転載先:https://www.cnblogs.com/DZzzz/p/10010530.html