Last active
April 13, 2019 14:28
-
-
Save jpsear/4043f28d8be42704d6013ebc03f7d893 to your computer and use it in GitHub Desktop.
Nest flat array that has children
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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