Skip to content

Instantly share code, notes, and snippets.

@JMWebDevelopment
Created December 19, 2016 02:52
Show Gist options
  • Save JMWebDevelopment/30fdc96e2a2726e5a7a0ed0a4be7db72 to your computer and use it in GitHub Desktop.
Save JMWebDevelopment/30fdc96e2a2726e5a7a0ed0a4be7db72 to your computer and use it in GitHub Desktop.
this.arrangeComments = function ( commentsList ) {
var maxDepth = 0;
for ( i = commentsList.length - 1; i >= 0; i-- ) {
if ( commentsList[i].comment_approved != 1 ) {
commentsList.splice( i, 1 );
}
}
for ( i = 0; i
< commentsList.length; i += 1 ) {
commentsList[i].comment_children = [];
var date = commentsList[i].comment_date.split(" ").join("T").concat("Z");
commentsList[i].comment_date = new Date(date);
commentsList[i].comment_depth = this.getCommentDepth( commentsList[i], commentsList );
if ( this.getCommentDepth( commentsList[i], commentsList ) > maxDepth ) {
maxDepth = this.getCommentDepth( commentsList[i], commentsList );
}
}
for ( i = maxDepth; i > 0; i-- ) {
for ( j = 0; j < commentsList.length; j++ ) {
if ( commentsList[j].comment_depth == i ) {
for ( k = 0; k < commentsList.length; k++ ) {
if ( commentsList[j].comment_parent == commentsList[k].comment_ID ) {
commentsList[k].comment_children.push( commentsList[j] )
}
}
}
}
}
for ( i = commentsList.length - 1; i >= 0; i-- ) {
if ( commentsList[i].comment_parent > 0 ) {
commentsList.splice( i, 1 );
}
}
return commentsList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment