zoukankan      html  css  js  c++  java
  • 处理特殊数据结构,转二维数组函数,活用

            let listArr = [
                {
                    id: 1,
                    name: "A",
                    childrenLabelInfo: [
                        {
                            id: 2,
                            name: "B",
                            childrenLabelInfo: [
                                {
                                    id: 3,
                                    name: "C"
                                },
                                {
                                    id: 4,
                                    name: "D"
                                }
                            ]
                        },
                        {
                            id: 5,
                            name: "E"
                        }
                    ]
                },
                {
                    id: 6,
                    name: "F",
                    childrenLabelInfo: [
                        {
                            id: 7,
                            name: "G",
                            childrenLabelInfo: [
                                {
                                    id: 8,
                                    name: "H"
                                },
                                {
                                    id: 9,
                                    name: "I"
                                }
                            ]
                        },
                        {
                            id: 10,
                            name: "G"
                        }
                    ]
                }
            ]
            let arrList = []
            listArr.forEach(item => {
                ArrItem(item, arrList)
            })
            function ArrItem(arrItem, arrLists) { // 保存第一层数据
                let arr = [];
                arr.push(arrItem.id);
                if (arrItem.childrenLabelInfo !== undefined) {
                    arrItem.childrenLabelInfo.forEach((item, index) => {
          // 深拷贝,转换引用类型地址
                        let arrStyle = JSON.parse(JSON.stringify(arr))
                        arrLists.push(Return(arrStyle, item, index, arrLists))
                    })
                } else {
                    arrLists.push(arr)
                }
            }
            function Return(arr, arrItem, index, arrLists) { // 处理其他层次的函数
                arr.push(arrItem.id)
                if (arrItem.childrenLabelInfo !== undefined) {
                    arrItem.childrenLabelInfo.forEach((item, ind) => {
                        let Star = JSON.parse(JSON.stringify(arr))
                        arrLists.push(Return(Star, item, ind, arrLists))
                    })
                } else {
                    return arr
                }
            }

            let arrTrue = []
            arrList.forEach(item => {
        // 处理无数据情况的undefined
                if (item !== undefined) {
                    arrTrue.push(item)
                }
            })

            console.log(arrTrue);
  • 相关阅读:
    Xhprof分析php性能
    Xhprof graphviz Warning: proc_open() [function.proc-open]: CreateProcess failed, error code 解决方法
    使用xhprof会在nginx下报502 Bad Gateway错误
    springbooot2 thymeleaf 配置以及加载资源文件。Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
    springboot用户登陆密码两次md5加密
    SpringBoot2.0 redis生成组建和读写配置文件
    spring2.0:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either th
    SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embe
    秋季学期总结
    寒假作业3:抓老鼠啊~亏了还是赚了?
  • 原文地址:https://www.cnblogs.com/supermanYU/p/14467796.html
Copyright © 2011-2022 走看看