2018-12-06 解决多异步请求,和树结构扁平化

1.解决for循环发异步请求,并确定全部请求返回后  开始回调

Promise.all(reqs).then(function (values) {

    //一次性执行完所有请求后回调

    values.forEach(function (res,index) {

        if (res.status ==200) {

            parentMenu[index].children=res.data.message;

        }

})

    vm.allMunuList=flatten(parentMenu);

    vm.menuList=vm.allMunuList;

});


2.树形结构json  扁平化处理

<script>

    var data=[

{

                "name":"栏目管理",

                "url":"/info/mng/type_mng.html",

                "mainType":false,

                "enabled":true,

                "children":[

]

            },

            {

                "name":"卫生保健",

                "typeId":"info12",

                "url":"/app/index.html#/application/notice?typeId=info12",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"每周食谱",

                        "typeId":"info1201",

                        "url":"/app/index.html#/application/notice?typeId=info1201",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"幼儿出勤",

                        "typeId":"info1204",

                        "url":"/app/index.html#/application/notice?typeId=info1204",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"膳食分析",

                        "typeId":"info1202",

                        "url":"/app/index.html#/application/notice?typeId=info1202",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"因病缺席幼儿跟踪",

                        "typeId":"info1203",

                        "url":"/app/index.html#/application/notice?typeId=info1203",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"园内通知",

                "typeId":"info01",

                "url":"/app/index.html#/application/notice?typeId=info01",

                "mainType":true,

                "enabled":true,

                "children":[

{

                        "name":"上级通知",

                        "typeId":"info0101",

                        "url":"/app/index.html#/application/notice?typeId=info0101",

                        "mainType":false,

                        "enabled":true,

                        "children":[

{

                                "name":"333333",

                                "typeId":"info010101",

                                "url":"/app/index.html#/application/notice?typeId=info010101",

                                "mainType":false,

                                "enabled":true

                            }

]

                    },

                    {

                        "name":"行政通知",

                        "typeId":"info0102",

                        "url":"/app/index.html#/application/notice?typeId=info0102",

                        "mainType":false,

                        "enabled":true

                    }

]

            },

            {

                "name":"校园动态",

                "typeId":"info13",

                "url":"/app/index.html#/application/notice?typeId=info13",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"校园动态",

                        "typeId":"info1301",

                        "url":"/app/index.html#/application/notice?typeId=info1301",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"党建工作",

                "typeId":"info02",

                "url":"/app/index.html#/application/notice?typeId=info02",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"党员学习",

                        "typeId":"info0202",

                        "url":"/app/index.html#/application/notice?typeId=info0202",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"党务公开",

                        "typeId":"info0201",

                        "url":"/app/index.html#/application/notice?typeId=info0201",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"党建文件",

                        "typeId":"info0203",

                        "url":"/app/index.html#/application/notice?typeId=info0203",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"人力资源",

                "typeId":"info10",

                "url":"/app/index.html#/application/notice?typeId=info10",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"职称评聘",

                        "typeId":"info1002",

                        "url":"/app/index.html#/application/notice?typeId=info1002",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"人员异动",

                        "typeId":"info1003",

                        "url":"/app/index.html#/application/notice?typeId=info1003",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"人事信息",

                        "typeId":"info1001",

                        "url":"/app/index.html#/application/notice?typeId=info1001",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"获奖登记",

                        "typeId":"info1004",

                        "url":"/app/index.html#/application/notice?typeId=info1004",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"安全保卫",

                "typeId":"info11",

                "url":"/app/index.html#/application/notice?typeId=info11",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"安全保卫",

                        "typeId":"info1101",

                        "url":"/app/index.html#/application/notice?typeId=info1101",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"督导信息",

                "typeId":"info05",

                "url":"/app/index.html#/application/notice?typeId=info05",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"记录表",

                        "typeId":"info0501",

                        "url":"/app/index.html#/application/notice?typeId=info0501",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"公示表",

                        "typeId":"info0502",

                        "url":"/app/index.html#/application/notice?typeId=info0502",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"课程教学",

                "typeId":"info06",

                "url":"/app/index.html#/application/notice?typeId=info06",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"家园活动",

                        "typeId":"info0605",

                        "url":"/app/index.html#/application/notice?typeId=info0605",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"班级管理",

                        "typeId":"info0606",

                        "url":"/app/index.html#/application/notice?typeId=info0606",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"读书分享",

                        "typeId":"info0603",

                        "url":"/app/index.html#/application/notice?typeId=info0603",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"教师随笔",

                        "typeId":"info0604",

                        "url":"/app/index.html#/application/notice?typeId=info0604",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"教师备课",

                        "typeId":"info0601",

                        "url":"/app/index.html#/application/notice?typeId=info0601",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"年级活动",

                        "typeId":"info0602",

                        "url":"/app/index.html#/application/notice?typeId=info0602",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"园务公开",

                "typeId":"info03",

                "url":"/app/index.html#/application/notice?typeId=info03",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"工程建设",

                        "typeId":"info0308",

                        "url":"/app/index.html#/application/notice?typeId=info0308",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"学期计划、总结",

                        "typeId":"info0301",

                        "url":"/app/index.html#/application/notice?typeId=info0301",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"周工作安排",

                        "typeId":"info0303",

                        "url":"/app/index.html#/application/notice?typeId=info0303",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"月工作安排",

                        "typeId":"info0302",

                        "url":"/app/index.html#/application/notice?typeId=info0302",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"每月大事记",

                        "typeId":"info0305",

                        "url":"/app/index.html#/application/notice?typeId=info0305",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"活动快讯",

                        "typeId":"info0304",

                        "url":"/app/index.html#/application/notice?typeId=info0304",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"会议纪要",

                        "typeId":"info0307",

                        "url":"/app/index.html#/application/notice?typeId=info0307",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"月、学期考核",

                        "typeId":"info0306",

                        "url":"/app/index.html#/application/notice?typeId=info0306",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"工会活动",

                "typeId":"info04",

                "url":"/app/index.html#/application/notice?typeId=info04",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"工会活动",

                        "typeId":"info0401",

                        "url":"/app/index.html#/application/notice?typeId=info0401",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"教育科研",

                "typeId":"info09",

                "url":"/app/index.html#/application/notice?typeId=info09",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"课题组活动",

                        "typeId":"info0904",

                        "url":"/app/index.html#/application/notice?typeId=info0904",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"体育组活动",

                        "typeId":"info0905",

                        "url":"/app/index.html#/application/notice?typeId=info0905",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"月主题活动",

                        "typeId":"info0902",

                        "url":"/app/index.html#/application/notice?typeId=info0902",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"教研组活动",

                        "typeId":"info0903",

                        "url":"/app/index.html#/application/notice?typeId=info0903",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"保育教研",

                        "typeId":"info0906",

                        "url":"/app/index.html#/application/notice?typeId=info0906",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"教研通知",

                        "typeId":"info0901",

                        "url":"/app/index.html#/application/notice?typeId=info0901",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"培训研修",

                "typeId":"info07",

                "url":"/app/index.html#/application/notice?typeId=info07",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"家长学校",

                        "typeId":"info0706",

                        "url":"/app/index.html#/application/notice?typeId=info0706",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"草根讲坛",

                        "typeId":"info0704",

                        "url":"/app/index.html#/application/notice?typeId=info0704",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"见习生管理",

                        "typeId":"info0705",

                        "url":"/app/index.html#/application/notice?typeId=info0705",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"继续教育",

                        "typeId":"info0702",

                        "url":"/app/index.html#/application/notice?typeId=info0702",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"外出学习",

                        "typeId":"info0703",

                        "url":"/app/index.html#/application/notice?typeId=info0703",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"青年教师培训",

                        "typeId":"info0701",

                        "url":"/app/index.html#/application/notice?typeId=info0701",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"专题活动",

                "typeId":"info08",

                "url":"/app/index.html#/application/notice?typeId=info08",

                "mainType":false,

                "enabled":true,

                "children":[

{

                        "name":"开放日活动",

                        "typeId":"info0803",

                        "url":"/app/index.html#/application/notice?typeId=info0803",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"活动简报",

                        "typeId":"info0804",

                        "url":"/app/index.html#/application/notice?typeId=info0804",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"教研活动",

                        "typeId":"info0801",

                        "url":"/app/index.html#/application/notice?typeId=info0801",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

                    },

                    {

                        "name":"专题组活动",

                        "typeId":"info0802",

                        "url":"/app/index.html#/application/notice?typeId=info0802",

                        "mainType":false,

                        "enabled":true,

                        "children":[

]

}

]

            },

            {

                "name":"公文管理",

                "typeId":"offi01",

                "url":"/app/index.html#/application/archives?typeId=offi01",

                "mainType":true,

                "enabled":true,

                "children":[

]

            },

            {

                "name":"内部审批",

                "typeId":"docu01",

                "url":"/app/index.html#/application/file?typeId=docu01",

                "mainType":true,

                "enabled":true,

                "children":[

]

}

        ];

    function flatten (data,blank=6) {

        return data.reduce((arr, {name, typeId, url, mainType,enabled,children =[]}) =>

arr.concat([{blank,name, typeId, url, mainType,enabled}], flatten(children,blank+2)), [])

}

    // function flatten (data, level = 0) {

//    return data.reduce((arr, {Id, Name, Pid, id, children = []}) =>

//    arr.concat([{level, Id, Name, Pid, id}], flatten(children, level+1)), [])

// }

    function aaa() {

        console.log(data)

      var ss=flatten(data);

        console.log(ss);

    }

</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,123评论 6 528
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,850评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,839评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,389评论 1 308
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,183评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,717评论 1 320
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,797评论 3 436
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,951评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,459评论 1 330
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,431评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,573评论 1 365
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,123评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,823评论 3 344
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,220评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,475评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,155评论 3 387
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,560评论 2 370