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

tableToTree helper #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrey-tushev
Copy link

@andrey-tushev andrey-tushev commented Sep 3, 2023

A helper that transforms database-like tables

[]{
    {1, 0, "A"},
    {2, 1, "AA"},
    {3, 1, "AB"},
    {4, 3, "ABA"},
    {5, 1, "AC"},
    {6, 0, "B"},
}

into tree-structures

[]{
    {value: "A", leafs: []{
        {value: "AA"},
        {value: "AB", leafs: []{
             {value: "ABA"},
        }},
        {value: "AC"},
    }},
    {value: "B"}
}

For example: table of content, shop categories or any other hierarchy

@lesichkovm
Copy link

In most cases you would still need the ID and the Parent ID in the tree nodes. May be just the example does not. Does this retain this info?

Also in most of the similar libraries the used word is "children" not "leafs". Best to keep consistent and use "children".

Also can you provide the reverse function from a Tree structure to Flattened-Tree, This would be helpful when needing to store it back?

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

Successfully merging this pull request may close these issues.

2 participants