Including the declaration of the 2d Array, how can I make the following recursive? I would like it to iterate until there are no "children()".
self.buildRelationships = function () {
relationships = [];
relationships[0] = [];
relationships[1] = [];
relationships[2] = [];
relationships[3] = [];
relationships[0][0] = self.name();
$.each(self.children(), function(i, child) {
relationships[1][i] = child.name();
$.each(child.children(), function (j, grandchild) {
relationships[2][j] = grandchild.name();
$.each(grandchild.children(), function (k, greatgrandchild) {
relationships[3][k] = greatgrandchild.name();
})
})
})
}
Bob.children() would return "James" and "Steve".
Me!.children() would return "Bob" and "Bill".
relationships[0] = "Me!" //this will always have a length of only 1
relationships[1] = "Bob", "Bill"
relationships[2] = "James", "Steve", "Fred"
relationships[3] = "Dale", "Owen", "Patrick"
self.buildRelationships = function () {
relationships = [];
relationships[0] = [];
relationships[1] = [];
relationships[2] = [];
relationships[3] = [];
relationships[0][0] = self.name();
var recursive = function(level) {
return function(i, child) {
relationships[level] = relationships[level] || [];
relationships[level].push(child.name());
$.each(child.children(), recursive(level + 1));
}
}
$.each(self.children(), recursive(1));
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments