title: Looping through JSON children objects
body: <p>So I am looping through mid-complex json object and saving all the values when I reach the end of recursive loop</p>
  
  <p>Object e.g.</p>
  
  <pre><code>"if": {
      "and": {
          "or": {
              "compare": [
                  {
                      "Hello": {
                          "constant": {
                              "string": "1"
                          }
                      },
                  },
                  {
                      "Hello": {
                          "constant": {
                              "string": "4"
                          }
                      }
                  }
              ]
          },
          "before": {
              "Hello2": "12131",
              "Hello": {
                  "constant": {
                      "datetime": "2001-01-01T00:00:00"
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Then I have my function: </p>
  
  <pre><code>function getAllGroups(obj)
  {
  
          for (var k in obj)
          {
              if (k === "hello") {
                  counter++;
                  array.push([]);
              }
              if (typeof obj[k] == "object" &amp;&amp; obj[k] !== null) {
                  getAllGroups(obj[k]);
              }
              else
              {
                  if (k !== "hello2") {
                      array[array.length - 1].push(obj[k]);
                  }
              }
  
          }
  }
  </code></pre>
  
  <p>It might make no sense right now but basically:</p>
  
  <p>When it finds key "hello" I add new object to my empty array and populate that object with data. Everytime it reaches key "hello" it will create new object and populate it with new data.</p>
  
  <p>So I got stuck at keeping reference to the parent object. There are few keys that I want to maintain e.g. "and" &amp; "or". Therefore I have first end of loop which stops at string: 1 I want to maintain that it is part of "or". Similarly, I want to keep that "or" is part of "and". Lastly that datetime is part of "and". Keep in mind that I can have multiple "and" and "or" within each other.</p>
  
  <p>EDIT:</p>
  
  <p>I have changed to code so that it will keep the reference to the parent. Unfortunately it will keep reference to the last parent in the list, therefore "datetime" is child of "and", but my code shows its child of "or"</p>
  
  <pre><code>function getAllGroups(obj)
  {
  
          for (var k in obj)
          {
              if (k === "and" || k === "or" || k === "not")
              {
                  if (parentKey !== "") {
                      array.push([]);
                      array[array.length - 1].push(array[array.length - 1]['parent'] = parentKey);
                      parentKey = k + parentKeyNo;
                      parentKeyNo++;
                      array[array.length - 1].push(array[array.length - 1]['child'] = parentKey);
                  }
                  else {
                      parentKey = k + parentKeyNo;
                      parentKeyNo++;
                      array.push([]);
                      array[array.length - 1].push(array[array.length - 1]['child'] = parentKey);
                  }
              }
              if (k === "Hello") {
                  counter++;
                  array.push([]);
              }
              if (typeof obj[k] == "object" &amp;&amp; obj[k] !== null) {
                  getAllGroups(obj[k]);
              }
              else
              {
                  if (k !== "Hello2") {
                      if (array[array.length - 1].hasOwnProperty('parent'))
                      {
                          array[array.length - 1].push(obj[k]);
                      }
                      else
                      {
                          array[array.length - 1].push(array[array.length - 1]['parent'] = parentKey);
                          array[array.length - 1].push(obj[k]);
                      }
  
                  }
              }
  
          }
  }
  </code></pre>
  
  <p>DESIRED RESULT:</p>
  
  <pre><code>[
      [
          {
              "child": "and"
          }
      ],
      [
          {
              "parent": "and"
          },
          {
              "child": "or"
          }
      ],
      [
          {
              "parent": "or"
          },
          {
              "string": "1"
          }
      ],
      [
          {
              "parent": "or"
          },
          {
              "string": "4"
          }
      ],
      [
          {
              "parent": "and"
          },
          {
              "datetime": "2001-01-01T00:00:00"
          }
      ]
  ]
  </code></pre>