• converts a nested tree structure into a flat list structure

    Example

    import { tree2List } from "tree-handle-lib";
    const inputTree = [
    {
    key: 1,
    name: "Parent 1",
    children: [
    {
    key: 2,
    name: "Child 1-1",
    children: [],
    },
    {
    key: 3,
    name: "Child 1-2",
    children: [
    {
    key: 4,
    name: "Grandchild 1-2-1",
    children: [],
    },
    ],
    },
    ],
    },
    {
    key: 5,
    name: "Parent 2",
    children: [
    {
    key: 6,
    name: "Child 2-1",
    children: [],
    },
    ],
    },
    ];
    const _result = tree2List(inputTree);
    // [
    // { 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 },
    // ];

    Parameters

    • node: TNode

      the root node of the tree, can be a single node or an array of nodes

    • 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 TListNode[]

Generated using TypeDoc