Skip to content

Instantly share code, notes, and snippets.

@junajan
Created July 13, 2016 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junajan/05ad8315f63c9e90ad8373eb4eb75fd8 to your computer and use it in GitHub Desktop.
Save junajan/05ad8315f63c9e90ad8373eb4eb75fd8 to your computer and use it in GitHub Desktop.
// idComment, idParentComment, idArticle, userName, userComment, dateAdd
var comments = [
{
id: 1,
parent_id: 0,
article_id: 1,
user_name: "Jiffy",
user_text: "Viva la vida"
},
{
id: 2,
parent_id: 1,
article_id: 1,
user_name: "Lisa",
user_text: "Me gusta"
},
{
id: 3,
parent_id: 2,
article_id: 1,
user_name: "Romeo",
user_text: "Ahh say no more"
},
{
id: 4,
parent_id: 1,
article_id: 1,
user_name: "Julia",
user_text: "I am here"
},
{
id: 5,
parent_id: 0,
article_id: 1,
user_name: "Buffy",
user_text: "New root comment"
}
];
// this will create basic structures
function treeInit(comments) {
/*
Parent structure:
{ '0': [ 1, 5 ], '1': [ 2, 4 ], '2': [ 3 ] }
*/
var idParentMap = {};
/*
List of comments indexed by ids
{ 1: .., 2: .., ...}
*/
var commentsObjected = {};
comments.forEach(function (item) {
commentsObjected[item.id] = item;
if (!idParentMap[item.parent_id])
idParentMap[item.parent_id] = [];
idParentMap[item.parent_id].push(item.id);
});
return {idParentMap, commentsObjected};
}
function treeRecursion(map, comments, layer) {
var treeLayer = [];
if(!layer) return [];
// go through this parent and put all its childrens to childs array
layer.forEach(function (id) {
comments[id].childs = treeRecursion(map, comments, map[id]);
treeLayer.push(comments[id]);
});
return treeLayer;
}
// create tree structure from flat array
function tree(comments) {
var tree = [];
// first go through array and create id map of childs and comment list indexed by comment id
var {idParentMap, commentsObjected} = treeInit(comments);
console.log("Id map:", idParentMap);
console.log("Comments Objected:", commentsObjected);
// if there is some comment in root of the tree (parent = 0) start recursion
if(idParentMap[0]) {
tree = treeRecursion(idParentMap, commentsObjected, idParentMap[0]);
}
return tree;
}
// ===============================
var util = require("util");
var c = tree(comments);
console.log(util.inspect(c, false, null));
// outputs:
[nodemon] starting `node tree.js`
Id map: { '0': [ 1, 5 ], '1': [ 2, 4 ], '2': [ 3 ] }
Comments Objected: { '1':
{ id: 1,
parent_id: 0,
article_id: 1,
user_name: 'Jiffy',
user_text: 'Viva la vida' },
'2':
{ id: 2,
parent_id: 1,
article_id: 1,
user_name: 'Lisa',
user_text: 'Me gusta' },
'3':
{ id: 3,
parent_id: 2,
article_id: 1,
user_name: 'Romeo',
user_text: 'Ahh say no more' },
'4':
{ id: 4,
parent_id: 1,
article_id: 1,
user_name: 'Julia',
user_text: 'I am here' },
'5':
{ id: 5,
parent_id: 0,
article_id: 1,
user_name: 'Buffy',
user_text: 'New root comment' } }
[ { id: 1,
parent_id: 0,
article_id: 1,
user_name: 'Jiffy',
user_text: 'Viva la vida',
childs:
[ { id: 2,
parent_id: 1,
article_id: 1,
user_name: 'Lisa',
user_text: 'Me gusta',
childs:
[ { id: 3,
parent_id: 2,
article_id: 1,
user_name: 'Romeo',
user_text: 'Ahh say no more',
childs: [] } ] },
{ id: 4,
parent_id: 1,
article_id: 1,
user_name: 'Julia',
user_text: 'I am here',
childs: [] } ] },
{ id: 5,
parent_id: 0,
article_id: 1,
user_name: 'Buffy',
user_text: 'New root comment',
childs: [] } ]
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node tree.js`
Id map: { '0': [ 1, 5 ], '1': [ 2, 4 ], '2': [ 3 ] }
Comments Objected: { '1':
{ id: 1,
parent_id: 0,
article_id: 1,
user_name: 'Jiffy',
user_text: 'Viva la vida' },
'2':
{ id: 2,
parent_id: 1,
article_id: 1,
user_name: 'Lisa',
user_text: 'Me gusta' },
'3':
{ id: 3,
parent_id: 2,
article_id: 1,
user_name: 'Romeo',
user_text: 'Ahh say no more' },
'4':
{ id: 4,
parent_id: 1,
article_id: 1,
user_name: 'Julia',
user_text: 'I am here' },
'5':
{ id: 5,
parent_id: 0,
article_id: 1,
user_name: 'Buffy',
user_text: 'New root comment' } }
[ { id: 1,
parent_id: 0,
article_id: 1,
user_name: 'Jiffy',
user_text: 'Viva la vida',
childs:
[ { id: 2,
parent_id: 1,
article_id: 1,
user_name: 'Lisa',
user_text: 'Me gusta',
childs:
[ { id: 3,
parent_id: 2,
article_id: 1,
user_name: 'Romeo',
user_text: 'Ahh say no more',
childs: [] } ] },
{ id: 4,
parent_id: 1,
article_id: 1,
user_name: 'Julia',
user_text: 'I am here',
childs: [] } ] },
{ id: 5,
parent_id: 0,
article_id: 1,
user_name: 'Buffy',
user_text: 'New root comment',
childs: [] } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment