Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TypeScript「Recursive processing if children are a in parent」parentの中にchildrenがaったらさらに再帰処理

TypeScript「Recursive processing if children are a in parent」parentの中にchildrenがあったらさらに再帰処理

type is here.

type Group = {
id: number;
value: string;
parentId: number;
children: Group[];
}
   const re = (group: Group): number[] => {
        return [
            group.id,
            ...(group.children?.map(re).reduce((p, c) => [...p, ...c], []) ||
                [])
        ]
    }
    const targetIds = re(values.group)

kenjimorita

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment