Can anyone help converting the following list of parent-child objects:
[ { \"name\":\"root\", \"_id\":\"root_id\", }, { \"name\":\"a1\"
I know I'm too late, but since I just finished my contribution to a sample implementation of how this can be done I thought I would share it, since it might be found useful / or give inspiration to an alternative solution.
The implementation can be found here: http://jsfiddle.net/sw_lasse/9wpHa/
The main idea of the implementation centers around the following recursive function:
// Get parent of node (recursive)
var getParent = function (rootNode, rootId) {
if (rootNode._id === rootId)
return rootNode;
for (var i = 0; i < rootNode.children.length; i++) {
var child = rootNode.children[i];
if (child._id === rootId)
return child;
if (child.children.length > 0)
var childResult = getParent(child, rootId);
if (childResult != null) return childResult;
}
return null;
};
... that is used to build the tree.