http://jsfiddle.net/eYgGK/
I stole this script from another post:
function convertToHierarchy() { var arry = [{ "Id": "1", "Name": "abc", "Parent": "", "attr": "abc" }, { "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc" }, { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" }, { "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }]; var nodeObjects = createStructure(arry); for (var i = nodeObjects.length - 1; i >= 0; i--) { var currentNode = nodeObjects[i]; if (currentNode.value.Parent === "") { continue; } var parent = getParent(currentNode, nodeObjects); if (parent === null) { continue; } parent.children.push(currentNode); nodeObjects.splice(i, 1); } console.dir(nodeObjects); return nodeObjects; } function createStructure(nodes) { var objects = []; for (var i = 0; i < nodes.length; i++) { objects.push({ value: nodes[i], children: [] }); } return objects; } function getParent(child, nodes) { var parent = null; for (var i = 0; i < nodes.length; i++) { if (nodes[i].value.Id === child.value.Parent) { return nodes[i]; } } return parent; }
This script produces:
[{ "value": { "Id": "1", "Name": "abc", "Parent": "", "attr": "abc" }, "children": [{ "value": { "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc" }, "children": [{ "value": { "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }, "children": [] }, { "value": { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" }, "children": [] }] }] }]
I'm looking for:
[{ "Id": "1", "Name": "abc", "Parent": "", "attr": "abc", "children": [{ "Id": "2", "Name": "abc", "Parent": "1", "attr": "abc", "children": [{ "Id": "4", "Name": "abc", "Parent": "2", "attr": "abc" }, { "Id": "3", "Name": "abc", "Parent": "2", "attr": "abc" }] }] }]
I need to get rid of the "value" wrapper first, and the empty one - node. I know I can write a cleanup script, but that would be less than best practice. It would be great if someone knows how to fix, or suggest a different script!
thanks