Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active December 27, 2019 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/a71635c904488f852c0993fa3a5931a2 to your computer and use it in GitHub Desktop.
Save kenmori/a71635c904488f852c0993fa3a5931a2 to your computer and use it in GitHub Desktop.
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