Skip to content

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

@Rain120

Description

@Rain120

题目

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: [],
                                    },
                                ],
                            },
                        ],
                    },
                ],
            },
        ],
    },
];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions