• Given a flat array of objects linked to one another, it will nest them recursively

    Example

    const comments = [
    { id: 1, comment_id: null },
    { id: 2, comment_id: 1 },
    { id: 3, comment_id: 1 },
    { id: 4, comment_id: 2 },
    { id: 5, comment_id: 4 }
    ];
    nest(comments, "comment_id"); // [{ id: 1, comment_id: null, children: [...] }]

    Type Parameters

    • T extends Record<string, unknown>

    Parameters

    • arr: T[]
    • Optional link: string = "parent_id"
    • Optional key: string = "id"

    Returns Record<string, unknown>[]

Generated using TypeDoc