How can I make this recursive?

Devil's Advocate

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();
                })
            })
        })
    }

Visualization of objective

Given this data:

  • Me!
    • Bob
      • James
        • Dale
      • Steve
    • Bill
      • Fred
        • Owen
        • Patrick

.children() returns immediate children only:

Bob.children() would return "James" and "Steve".

Me!.children() would return "Bob" and "Bill".

The accepted answer would create data that looked like this:

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"
Mongus Pong
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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make recursive query in SQL?

From Dev

How can I make recursive query in SQL?

From Dev

How can I make this code tail recursive?

From Dev

How can I make this recursive function more efficient?

From Dev

How can I make a retry function tail recursive?

From Dev

How can I make my recursive C program more efficient?

From Dev

How can I make the following function into a tail-recursive function?

From Dev

How can I make this query recursive Sql Server?

From Dev

How can I make my crawler do recursive call?

From Dev

How can I make this recursive Binary Tree Traversal return null if the input is not found in the tree?

From Dev

PHP - How can I create a recursive function?

From Dev

How can I detect recursive arrays and hashes?

From Dev

How can I have a recursive mutable borrow?

From Dev

How can I write this recursive Haskell function

From Dev

How can fixed-point combinators make recursive constructs terminate?

From Dev

recursive function for upheap method(how can i write this non-recursive method into recursive)

From Dev

How to make a recursive method?

From Dev

How would I make a recursive trace print out like this?

From Dev

How do I make jQuery wait for a recursive function to finish?

From Dev

How do I make jQuery wait for a recursive function to finish?

From Dev

How do I make this rsync command non-recursive?

From Dev

How can I make this repeat?

From Dev

How can I make slope

From Dev

How can I make this sophistictaed?

From Dev

How can I make this Serializable?

From Dev

How can i make animation?

From Dev

How can I make this sophistictaed?

From Dev

How can I make this repeat?

From Dev

How can i make this work

Related Related

HotTag

Archive