title: Recursive javascript function to return array
body: <p>I have an array of objects. The objects may have a property called children which inturn would include an array of objects.
  It is effectively a tree of objects. </p>
  
  <pre><code>var my_array_of_objects=[
    {
      "object_info": "blah blah",
      "children": [
        {
          "object_info": "blah blah",
          "children": [
            {
              "object_info": "blah blah"
            }
          ]
        }
      ]
    },
    {
      "object_info": "blah blah"
    },
    {
      "object_info": "blah blah"
    }
  ]
  </code></pre>
  
  <p>I am trying to recursively loop through the my_array_of_objects to return an array that will remove the tree architecture and give each object a relationship_id and a parent_relationship_id, however I am having a problem with the relationship_id getting set before the recursions happen and I can't see why... I assume it is a scoping issue?</p>
  
  <pre><code>  function sort_relationships(objects, return_objects, parent_relationship_id, relationship_id){
        $.each( objects, function( field_key, object ){
          relationship_id++;
          var new_object={};
          new_object.relationship_id=relationship_id;
          new_object.parent_relationship_id=parent_relationship_id;
          new_object.object_info=object.object_info;
          // PUSH OBJECT TO THE ARRAY TO BE RETURNED
          return_objects.push(new_object);
          // IF THE OBJECT HAS CHILDREN THEN RUN THE FUNCTION AGAIN WITH IT'S OBJECTS
          if(object.children instanceof Array){
            sort_relationships(object.children,return_objects,new_object.relationship_id,relationship_id);
          }
        });
        return return_objects;
    }
  </code></pre>
  
  <p>var my_new_array=sort_relationships(my_array_of_objects,[],0,0);</p>
  
  <p>Based on the code above I would expect to have a unique relationship_id for each new object, however it is returning the following.</p>
  
  <pre><code>[
    {
      "relationship_id": 1,
      "parent_relationship_id": 0,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 2,
      "parent_relationship_id": 1,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 3,
      "parent_relationship_id": 2,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 2,
      "parent_relationship_id": 0,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 3,
      "parent_relationship_id": 0,
      "object_info": "blah blah"
    }
  ]
  </code></pre>
  
  <p>I would expect to have the following:</p>
  
  <pre><code>[
    {
      "relationship_id": 1,
      "parent_relationship_id": 0,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 2,
      "parent_relationship_id": 1,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 3,
      "parent_relationship_id": 2,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 4,
      "parent_relationship_id": 0,
      "object_info": "blah blah"
    },
    {
      "relationship_id": 5,
      "parent_relationship_id": 0,
      "object_info": "blah blah"
    }
  ]
  </code></pre>