铺平多维对象数组 js

    const array = [{
            id: 1,
            title: "课程 1",
            children: [{
                    id: 4,
                    title: "课程 1-1"
                },
                {
                    id: 5,
                    title: "课程 1-2",
                    children: [{
                            id: 6,
                            title: "课程 1-2-1"
                        },
                        {
                            id: 7,
                            title: "课程 1-2-2"
                        },
                    ],
                },
            ],
        },
        {
            id: 2,
            title: "课程 2"
        },
        {
            id: 3,
            title: "课程 3"
        },
    ];

    //感觉方法很多  递归调用
    function steamroller(arr) {
        var newArr = [];
        arr.forEach(element => {
            newArr.push(element)
            if (element.children) {
                newArr.push.apply(newArr, steamroller(element.children))
                delete element.children
            } else {

            }
        });
        return newArr
    }
    console.log(steamroller(array))
君不见,高堂明镜悲白发,朝如青丝暮成雪
原文地址:https://www.cnblogs.com/lzhflzjx/p/13986059.html