SOURCE

let tree = [
    {
        id: 1, title: "1", children: [
            { id: 2, title: "1-1" },
            { id: 3, title: "1-2", children: [{ id: 4, title: "1-2-1", children: [{ id: 5, tile: "1-2-1-1" }] }] },
            { id: 6, title: "1-3", children: [{ id: 7, title: "1-3-1" }] }
        ]
    },
    { id: 8, title: '2' },
    { id: 9, title: '3', children: [{ id: 10, title: '3-1' }] }
]

/**
 * 树结构转扁平结构
 */
function getFlattenData(data) {
    data = JSON.parse(JSON.stringify(data))
    let result = []

    function fn(arr) {
        for (let i of arr) {
            result.push(i)
            if (i.children && i.children.length !== 0) {
                fn(i.children)
                delete i.children
            }
        }
    }

    fn(data)

    return result
}

console.log(getFlattenData(tree))

/**
 * 根据index删除节点
 */
function deleteNodeByIndex(arr, index) {
    let count = 0
    let returnFlag = false
    function fn(arr) {
        for (let i = 0; i < arr.length; i++) {
            if (returnFlag) { // 终止递归
                return
            }
            if (count === index) {
                arr.splice(i, 1)
                returnFlag = true
                return
            }
            count++
            if (arr[i].children && arr[i].children.length !== 0) {
                fn(arr[i].children)
            }
        }
    }
    fn(arr)
}

deleteNodeByIndex(tree, 3)
console.log(tree)
console 命令行工具 X clear

                    
>
console