Function flattenTreeArray

  • 扁平化树结构数组

    Type Parameters

    Parameters

    • treeData: T[]

      树结构数组

    • childrenFieldName: keyof T = 'children'

      子节点字段名,默认为 'children'

    Returns T[]

    扁平化后的树结构数组

    interface MyTreeNode {
    id: number;
    name: string;
    children?: MyTreeNode[];
    }

    const treeData: MyTreeNode[] = [
    {
    id: 1,
    name: 'Node 1',
    children: [
    {
    id: 2,
    name: 'Node 2',
    },
    {
    id: 3,
    name: 'Node 3',
    children: [
    {
    id: 4,
    name: 'Node 4',
    },
    {
    id: 5,
    name: 'Node 5',
    },
    ],
    },
    ],
    },
    {
    id: 6,
    name: 'Node 6',
    },
    ];

    const flattenedData = flattenTreeArray(treeData);