Given the following array of json objects:
var items = [
{id:1, parentId:0, name:'item1'},
{id:2, parentId:1, name:'item1.2'},
{id:3, parentId:1, name:'item1.3'},
{id:4, parentId:3, name:'item3.4'},
{id:5, parentId:3, name:'item3.5'},
...more nesting, levels, etc.
];
And a base object like:
var myObj = function(id, parentId, name, children){
this.id = id;
this.parentId = parentId;
this.name = name;
this.children = children;
};
how do I use recursion to loop through this items array and build a new array like so:
var newArray = [
new myObj(1, 0, 'item1', [
new myObj(2, 1, 'item1.2', []),
new myObj(3, 1, 'item1.3', [
new myObj(4, 3, 'item3.4', []),
new myObj(5, 3, 'item3.5', [])
])
]);
];
any help is greatly appreciated
edit: the parent/child relationship can be infinite. so i'm looking for a recursion function that would convert the first "items" array into the second "newarray"