How do you create a JSON family tree

Hi All,
I would be very grateful if you could show how to create a JSON style data structure with the following attributes;

The structure would be a recursive structure like this

family ={
 name : "name of person",
 children : [{
    name : "name of person",
    children: [{
      name:
      children:[ ]
   }]
}

 specifically i would like to call an initator function that takes as arguments
the "name of the person" and the family_tree as an object;

so lets say the family tree is something like this
Micheal (Grand Father)
Joseph  (Father)
Junior   (Son)

the function would work something like this
var myfamily_tree = new family_tree()

add_node(myfamilytree,"Micheal");
add_node(myfamilytree,"Joseph");
add_node(myfamilytree,"Junior");

then could i access the tree something like this
myfamilytree.pop() = "Michael"
myfamilytree.pop() = "Joseph"
myfamilytree.pop() = "Junior"

Thanks in advance,
All help is much appreciated
JITchallengerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
To "start" : http://jsfiddle.net/ZDQef/
    // http://genealogygems.tv/Images/arbol_genealogico_simpsons.png

    function family_tree(name) {
        
        this.createFamily = function(name) {
            this.name = name;
            this.childrens = [];
        }
        
        this.add_node = function(name) {
            return this.childrens[this.childrens.push(new family_tree(name))-1];
        }
        
        this.createFamily(name);
    }

    var f = new family_tree("Simpson");
    var Abraham_ft = f.add_node("Abraham");
    var Homer_ft = Abraham_ft.add_node("Homer");
    Homer_ft.add_node("Bart");
    Homer_ft.add_node("Lisa");
    Homer_ft.add_node("Maggie");

// checking :
    alert( JSON.stringify(f) );

Open in new window

tree.jpg
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JITchallengerAuthor Commented:
Thanks leakim971 for that excellent code
Your definitely the champ here
<a href="http://www.youtube.com/watch?v=h0ivw7jrSm4"> Hey Ha Hoo Hey</a>
0
ProculopsisCommented:

// try this which includes your pop functionality:

function familyMember( name ) {

  this.name = name;

  this.children = [];

  this.addChild = function( member ) {
    this.children.push( member );
  };

  this.pop = function( name ) {
    if ( name == this.name ) return this;
    for ( var index = 0; index < this.children.length; index++ ) {
      var child = this.children[index];
      if ( name == child.pop( name ).name ) return child.pop( name );
    }
  };

  this.toString = function() { return JSON.stringify( this ); };

}

var familyTree = new familyMember( "Michael" );
var joseph = new familyMember( "Joseph" );
familyTree.addChild( joseph );

var junior = new familyMember( "Junior" );
joseph.addChild( junior );

alert( familyTree.pop("Junior").toString() );
0
leakim971PluritechnicianCommented:
You're welcome!
Have a nice sunday and Happy New Year!

http://jsfiddle.net/ZDQef/1/
this.pop = function() {
            return this.childrens.pop().name;
        }

Open in new window

0
GopinathPandianCommented:
Nice
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.