title: Javascript nested array get elements
body: <p>I have an array like:</p>
  
  <pre><code>"pages": [{
      "key": "1",
      "pages": [{
          "key": "2",
          "pages": [{
              "key": "3"
          }]
      },
          {
              "key": "4",
              "pages": [{
                  "key": "5"
              }]
  
          }]
  }]
  </code></pre>
  
  <p>where key 1 and 4 are at same level and 1 contains 2 which contains 3 and the key 4 contains 5. The result I want is in the order [3,2,5,1,4]. I have tried the following recursion but i am not able to get the correct order.</p>
  
  <pre><code> function fnGetAll (oTopDetailPage, array) {
      var i;
  
      for (i=0; i&lt;oTopDetailPage.length; i++) {
          array.push(oTopDetailPage[i]);
          if(oTopDetailPage[i].pages) {
              fnGetAllSubPages(oTopDetailPage[i].pages, array);
          }
      }
      return array;
  }
  </code></pre>