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

生成部门树 #50

Open
zhn4 opened this issue Aug 24, 2020 · 0 comments
Open

生成部门树 #50

zhn4 opened this issue Aug 24, 2020 · 0 comments

Comments

@zhn4
Copy link
Owner

zhn4 commented Aug 24, 2020

使用场景
一维对象数组,保存部门数据,需要根据上下级关系组建一棵目录树

参考
https://segmentfault.com/a/1190000018692367

demo
https://jsbin.com/lakucozoru/edit?js,console,output

function toTreeData(data,pid){
    function tree(id) {
        let arr = []
        data.filter(item => {
            return item.pid === id;
        }).forEach(item => {
            arr.push({
                id: item.id,
                name: item.name,
                children: tree(item.id)
            })
        })
        return arr
    }
    return tree(pid)  // 第一级节点的父id,是null或者0,视情况传入
}
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