Skip to content

Instantly share code, notes, and snippets.

@djfm
Created April 23, 2021 20:22
Show Gist options
  • Save djfm/284e5cc6d017f1276da0ba417e36efcd to your computer and use it in GitHub Desktop.
Save djfm/284e5cc6d017f1276da0ba417e36efcd to your computer and use it in GitHub Desktop.
// Flatten an array of nodes, returning all nodes
// of the tree without their children.
const flattenNodeTree = (node) => {
const { children, ...otherProps } = node;
if (!children) {
return [{ ...otherProps }];
}
return [{ ...otherProps }, ...[].concat(...children.map(flattenNodeTree))];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment