• transform list data to tree data

    Example

    import { list2Tree } from "tree-handle-lib";
    const list = [
    {
    key: 1,
    name: "Parent 1",
    parentKey: null,
    },
    {
    key: 2,
    name: "Child 1-1",
    parentKey: 1,
    },
    {
    key: 3,
    name: "Child 1-2",
    parentKey: 1,
    },
    {
    key: 4,
    name: "Grandchild 1-2-1",
    parentKey: 3,
    },
    {
    key: 5,
    name: "Parent 2",
    parentKey: null,
    },
    {
    key: 6,
    name: "Child 2-1",
    parentKey: 5,
    },
    ];
    const _result = list2Tree(list);
    // [
    // {
    // key: 1,
    // name: "Parent 1",
    // parentKey: null,
    // children: [
    // { key: 2, name: "Child 1-1", parentKey: 1, children: [] },
    // {
    // key: 3,
    // name: "Child 1-2",
    // parentKey: 1,
    // children: [
    // { key: 4, name: "Grandchild 1-2-1", parentKey: 3, children: [] },
    // ],
    // },
    // ],
    // },
    // {
    // key: 5,
    // name: "Parent 2",
    // parentKey: null,
    // children: [{ key: 6, name: "Child 2-1", parentKey: 5, children: [] }],
    // },
    // ];

    Parameters

    • list: TListNode[]

      the origin list data with parentKey

    • keyName: string = KEY_NAME

      the key name of the node in the tree structure,default value is KEY_NAME='key'

    • childrenName: string = CHILDREN_NAME

      the child key name of the child node in the tree structure,default value is CHILDREN_NAME='children'

    • parentKeyName: string = PARENT_KEY_NAME

      the parent key name of the node in the tree structure,default value is PARENT_KEY_NAME='parentKey'

    Returns TNode

Generated using TypeDoc