Skip to content

Instantly share code, notes, and snippets.

@DarwinniwraD
Last active December 3, 2019 13:47
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 DarwinniwraD/ad064d3858a403e257868dc15677903a to your computer and use it in GitHub Desktop.
Save DarwinniwraD/ad064d3858a403e257868dc15677903a to your computer and use it in GitHub Desktop.
array of objects to tree
export default const getUnflatten = (arr, idFieldName, parentFieldName) => (arr, parentid) => arr.reduce((all, item) => item[parentFieldName] === parentid ? all.concat({...item, children: getUnflatten(arr, item[idFieldName])}): all, [] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment