• 周二. 4月 23rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

js 树型数据 转 数组

admin

11月 28, 2021
/**
 * 树转数组扁平化结构   
 * 深度优先遍历  堆栈  后进先出
 */
function deep(node){
    let stack = node,
        data = [];
    while(stack.length != 0){
        let pop = stack.pop();
        data.push({
            id: pop.id,
            name: pop.name,
            parentId: pop.parentId
        })
        let children = pop.children
        if(children){
            for(let i = children.length-1; i >=0; i--){
                stack.push(children[i])
            }
        }
    }
    return data
}
//console.log(deep(res1))

/**
 * 树转数组扁平化结构   
 * 深度优先遍历  递归
 */
function deepTraversal(data) {
    const result = [];
    data.forEach(item => {
        const loop = data => {
            result.push({
                id: data.id,
                name: data.name,
                parentId: data.parentId
            });
            let child = data.children
            if(child){
                for(let i = 0; i < child.length; i++){
                    loop(child[i])
                }
            }
        }
        loop(item);
    })
    return result;
}
//console.log(deepTraversal(res1))

/**
 * 广度优先
 * 队列  先进先出
 */
function wideTraversal(node){
    let stack = node,
        data = [];
    while(stack.length != 0){
        let shift = stack.shift();
        data.push({
            id: shift.id,
            name: shift.name,
            parentId: shift.parentId
        })
        let children = shift.children
        if(children){
            for(let i = 0; i < children.length; i++){
                stack.push(children[i])
            }
        }
    }
    return data
}

《js 树型数据 转 数组》有一个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注