Created
March 15, 2019 05:54
-
-
Save asifvora/7e55661d7615ed512fcac32e707b6a7f to your computer and use it in GitHub Desktop.
Recursion JavaScript
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 = "parent_id") => | |
items | |
.filter(item => item[link] === id) | |
.map(item => ({ ...item, children: nest(items, item.id) })) | |
const comments = [ | |
{ id: 1, parent_id: null, text: "First reply to post." }, | |
{ id: 2, parent_id: 1, text: "First reply to comment #1." }, | |
{ id: 3, parent_id: 1, text: "Second reply to comment #1." }, | |
{ id: 4, parent_id: 3, text: "First reply to comment #3." }, | |
{ id: 5, parent_id: 4, text: "First reply to comment #4." }, | |
{ id: 6, parent_id: null, text: "Second reply to post." } | |
] | |
nest(comments) | |
/* | |
[ | |
{ id: 1, parent_id: null, text: "First reply to post.", children: [...] }, | |
{ id: 6, parent_id: null, text: "Second reply to post.", children: [] } | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment