Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

树操作 #9

Open
ziv-zjc opened this issue Jul 15, 2019 · 0 comments
Open

树操作 #9

ziv-zjc opened this issue Jul 15, 2019 · 0 comments

Comments

@ziv-zjc
Copy link
Owner

ziv-zjc commented Jul 15, 2019

树操作

遍历

/**
 * 广度遍历优先,获取叶子节点
 * @param {*} obj 
 * 
 * let obj = {a:1, b:"sdf", c:{d: 123, e: "vv"}, f:{g:{h:{i:"afs"}, j:4545}, k: 123}, l: 23 }
 */

function bfsTraverse(obj) {
    let leafList = []
    let stack = [...Object.values(obj)]

    while (stack.length) {
        let item = stack.shift()
        if (item && typeof item != 'object') {
            leafList.push(item)
        } else {
            Object.values(item).forEach((childValue) => {
                stack.push(childValue)
            })
        }
    }
    return leafList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant