Skip to content

Instantly share code, notes, and snippets.

@closetothe
Last active July 6, 2019 17:15
Show Gist options
  • Save closetothe/dc901e150ee2c4c35a9957ca1ce0c062 to your computer and use it in GitHub Desktop.
Save closetothe/dc901e150ee2c4c35a9957ca1ce0c062 to your computer and use it in GitHub Desktop.
A simple recursive function for flattening a tree of forum posts saved in MongoDB, preserving the correct order in which to display them (left order traversal). Used in notmoodle.com (app.js and thread.ejs).
var getPostPriority = async function(nodeRef, priority){
var node = await Post.findById(nodeRef)
priority.push(node);
for(var i = 0; i < node.children.length; i++)
await getPostPriority(node.children[i], priority);
return priority;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment