Skip to content

Instantly share code, notes, and snippets.

@f3ath
Last active January 26, 2017 05:01
Show Gist options
  • Save f3ath/d4dd80e785717a63cb0ba2c8cc66faf4 to your computer and use it in GitHub Desktop.
Save f3ath/d4dd80e785717a63cb0ba2c8cc66faf4 to your computer and use it in GitHub Desktop.
function TreeBuilder() {
var makeEmptyNode = function() {
return {
"_items": []
};
};
var root = makeEmptyNode();
this.addNode = function (node) {
var current = root;
node.path.map(function (pathItem) {
if (!current.hasOwnProperty(pathItem)) {
current[pathItem] = makeEmptyNode();
}
current = current[pathItem];
});
current._items.push(node);
};
this.getTree = function() {
return root;
}
}
var nodes = [
{
"id": 6022788483583,
"name": "Frequent International Travelers",
"description": "People who have traveled abroad more than once in the past six months",
"type": "behaviors",
"audience_size": 189974052,
"path": ["Behaviors", "Travel"]
},
{
"id": 6006521036425,
"name": "Leisure travelers",
"description": "26",
"type": "behaviors",
"audience_size": 38439600,
"path": ["Behaviors", "Travel"]
}
];
var builder = new TreeBuilder();
nodes.map(function (node) {
builder.addNode(node);
});
console.log(JSON.stringify(builder.getTree()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment