首页 > 编程语言 > 详细

处理特殊数据结构,转二维数组函数,活用

时间:2021-03-02 11:03:02      阅读:22      评论:0      收藏:0      [点我收藏+]
        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);

处理特殊数据结构,转二维数组函数,活用

原文:https://www.cnblogs.com/supermanYU/p/14467796.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!