Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created April 26, 2014 15:31
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 rlemon/0af8bf2e6c422deed647 to your computer and use it in GitHub Desktop.
Save rlemon/0af8bf2e6c422deed647 to your computer and use it in GitHub Desktop.
function parseTree(arr, parent, tree) {
var children = arr.filter(function (child) {
return child.parent === parent.name;
});
arr = arr.filter(function (child) {
var check = true;
for( var i = 0; i < children.length; i++ ) {
if( child === children[i] ) {
check = false;
}
}
return check;
});
if (children.length) {
if (parent.name === 'null') {
tree = children;
} else {
parent.children = children;
}
}
children.forEach(function (child) {
parseTree(arr, child);
});
return tree;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment