Last active
January 26, 2017 05:01
-
-
Save f3ath/d4dd80e785717a63cb0ba2c8cc66faf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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