programernc
programernc

Reputation: 9

Javascript looping through children recursively

Hopefully someone can help me with the problem I'm facing!

I have this object with properties:

var myObj = function () {
   var oName     =  null;
   var oType     =  null;
   var oMultiplicity = null;
   var children = new Array();
}

The object represents one node and children is an empty array that has to be filled with node's children and children's children etc..

Every child is represented by the same object and has the same properties.

How can I make a method inside this object that recursively will fill the array with the first node's children and then their children etc..?

The input parameter for the method has to be the object itself. Also I have to keep track of the parent.

Thanks in advance.

Upvotes: 0

Views: 110

Answers (1)

Nir Tzezana
Nir Tzezana

Reputation: 2342

This should give you a good place to start at:

var myObj = function () {
   var oName     =  null;
   var oType     =  null;
   var oMultiplicity = null;
   var children = new Array();
   var findMyChildren = () => {
        if (v.children.length>0) {
            children.forEach((v)=> {
            v.finyMyChildren();
        });
      }
   }
}

Upvotes: 1

Related Questions