Skip to content

Instantly share code, notes, and snippets.

@jpsear
Last active April 13, 2019 14:28
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 jpsear/4043f28d8be42704d6013ebc03f7d893 to your computer and use it in GitHub Desktop.
Save jpsear/4043f28d8be42704d6013ebc03f7d893 to your computer and use it in GitHub Desktop.
Nest flat array that has children
const nest = (items, id = null, link = 'childId') => {
return items
.filter(item => item[link] === id)
.map(item => ({ ...item, children: nest(items, item._id) }))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment