Skip to content

Instantly share code, notes, and snippets.

@AyoAlfonso
Created October 2, 2018 22:44
Show Gist options
  • Save AyoAlfonso/7c77f9ae40f6b99a9cd9503774b1cb2b to your computer and use it in GitHub Desktop.
Save AyoAlfonso/7c77f9ae40f6b99a9cd9503774b1cb2b to your computer and use it in GitHub Desktop.
A Function For Unfolding Comments on Firebase
let rootCommentsCard = []
let repliesCard = []
function arrangeComments(comments){
for (var commentId in comments) {
let parent = comments[commentId].parent
if (parent[0] == 'root') {
comments[commentId].commentId = commentId
comments[commentId].replies = []
rootCommentsCard.push(comments[commentId])
} else {
comments[commentId].commentId = commentId
repliesCard.push(comments[commentId])
}
}
rootCommentsCard.map(card =>{
repliesCard.forEach(reply=> {
if (reply.parent[0] == card.commentId) {
card.replies.push(reply)
}
})
})
}
arrangeComments(comments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment