-
-
Save rlemon/0af8bf2e6c422deed647 to your computer and use it in GitHub Desktop.
This file contains 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 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