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

实现transform函数,将对象转成类似树结构的数组 #26

Open
Rain120 opened this issue Mar 19, 2022 · 1 comment
Open

实现transform函数,将对象转成类似树结构的数组 #26

Rain120 opened this issue Mar 19, 2022 · 1 comment

Comments

@Rain120
Copy link
Owner

Rain120 commented Mar 19, 2022

题目

function transform(values) {
  // ...
}

transform({
    0: {
        username: '0',
        department: 'A-B-C',
    },
    1: {
        username: '1',
        department: 'A-B-D',
    },
    2: {
        username: '2',
        department: 'A-X-Y',
    },
    3: {
        username: '2',
        department: 'A-C-B-D-Y',
    },
});
[
    {
        name: 'A',
        parent: '',
        path: 'A',
        children: [
            {
                name: '0',
                parent: 'A',
                path: 'A-B',
                children: [
                    {
                        name: '0',
                        parent: 'A-B',
                        path: 'A-B-C',
                        children: [],
                    },
                    {
                        name: '1',
                        parent: 'A-B',
                        path: 'A-B-D',
                        children: [],
                    },
                ],
            },
            {
                name: '2',
                parent: 'A',
                path: 'A-X',
                children: [
                    {
                        name: '2',
                        parent: 'A-X',
                        path: 'A-X-Y',
                        children: [],
                    },
                ],
            },
            {
                name: '2',
                parent: 'A',
                path: 'A-C',
                children: [
                    {
                        name: '2',
                        parent: 'A-C',
                        path: 'A-C-B',
                        children: [
                            {
                                name: '2',
                                parent: 'A-C-B',
                                path: 'A-C-B-D',
                                children: [
                                    {
                                        name: '2',
                                        parent: 'A-C-B-D',
                                        path: 'A-C-B-D-Y',
                                        children: [],
                                    },
                                ],
                            },
                        ],
                    },
                ],
            },
        ],
    },
];
@Rain120
Copy link
Owner Author

Rain120 commented Mar 19, 2022

function transform(values) {
    const res = [];
    const arr = [];
    const pathPool = [];

    for (const key in values) {
        const {username, department} = values[key];
        const paths = department.split('-');

        for (let i = 0; i < paths.length; i++) {
            const path = paths[i];
            const curPath = paths.slice(0, i + 1).join('-');
            if (pathPool.includes(curPath)) {
                continue;
            }

            pathPool.push(curPath);

            obj.name = i === 0 ? path : username;
            obj.path = curPath;
            obj.parent = i < 1 ? '' : paths.slice(0, i).join('-');
            obj.children = [];

            arr.push(obj);
        }
    }

    const map = {};

    for (let item of arr) {
        map[item.path] = item;
    }

    for (let item of arr) {
        if (!item.parent) {
            res.push(item);
            continue;
        } else {
            const parent = map[item.parent];

            if (parent) {
                parent.children.push(item);
            }
        }
    }

    return res;
}

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