Skip to content

Instantly share code, notes, and snippets.

@alxpsr
Last active November 14, 2022 14:11
Show Gist options
  • Save alxpsr/b2f891fe234f0ff44353f8a5ad6e04a1 to your computer and use it in GitHub Desktop.
Save alxpsr/b2f891fe234f0ff44353f8a5ad6e04a1 to your computer and use it in GitHub Desktop.

Revisions

  1. alxpsr renamed this gist Nov 14, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. alxpsr revised this gist Nov 14, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,6 @@ function forEachDeep<T extends INestable<T>>(array: T[], callback: (item: T) =>
    forEachDeep(elem.nestedList, callback)
    }

    callback
    callback(item)
    })
    }
  3. alxpsr created this gist Nov 14, 2022.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    interface INestable<T> {
    nestedList?: T[];
    }

    interface Item extends INestable<Item> {
    id: number;
    nestedList?: Item[];
    }

    const recursiveStructure: Item[] = [
    {
    id: 1,
    nestedList: [
    {
    id: 11,
    },
    {
    id: 22,
    nestedList: [
    {
    id: 33,
    }
    ]
    }
    ]
    }
    ];

    function forEachDeep<T extends INestable<T>>(array: T[], callback: (item: T) => void): void {
    array.forEach((elem: T) => {
    if (elem.nestedList) {
    forEachDeep(elem.nestedList, callback)
    }

    callback
    })
    }