title: How to access the JSON's multi-level objects with jQuery
body: <p>In CodeIgniter, in PHP to create a nested-like elements for dropdown <code>&lt;select&gt;</code> I'am using this function:</p>
  
  <pre><code>function list_categories(&amp;$result, $cats, $sub = ''){    // &lt;- THIS
      foreach($cats as $cat){
  
          //$cat['category']-&gt;id = $sub.$cat['category']-&gt;title;
          $result[$cat['category']-&gt;id] = $sub.$cat['category']-&gt;title; // &lt;- THIS
  
          if( sizeof($cat['children']) &gt; 0 ){
              $sub2 = str_replace('&amp;mdash;&amp;rarr;&amp;nbsp;', '&amp;ndash;', $sub);
              $sub2.= '&amp;ndash;&amp;rarr;&amp;nbsp;';
  
              list_categories($result, $cat['children'], $sub2); // &lt;- THIS
          }
      }
  }
  // Make an array available outside the function
  $categoryData = array(0 =&gt; '-- '.lang('FORM_SELECT_CATTOP').' --');
  // Launch the function to fill out the array with nested sets
  list_categories($categoryData, $categories);
  </code></pre>
  
  <p>Which generates the HTML <code>&lt;select&gt;</code> list:</p>
  
  <p><img src="https://i.stack.imgur.com/IyzkP.jpg" alt="categories"></p>
  
  <p>I want to do the same from retrieved JSON in jQuery but can't find the answer to go deeper than first level of JSON objects with this jQuery function:</p>
  
  <pre><code>$.getJSON('../raw_get_categories/'+com_id_selected+'/0', function(data){
      var items = [];
  
      $.each(data, function(key, value, sub){
          items.push('&lt;option id="'+ key[0] + '"&gt;' + value[0] + '&lt;/option&gt;');
      });
  
      console.log(items);
  });
  </code></pre>
  
  <p>And this is the JSON I want to manage and retrieve the same values from it like in PHP code:</p>
  
  <pre><code>{
      "28": {
          "category": {
              "id": "28", // &lt;&lt;&lt; I need to retrieve THIS
              "pid": "0",
              "com_id": "2",
              "route_id": "59",
              "excerpt": "",
              "ordering": "1",
              "title": "Oblecenie a vybava",
              "slug": "oblecenie-a-vybava1",
              "description": "",
              "image": "clothes.png",
              "seo_title": "",
              "meta": ""
          },
          "compname": {
              "id": "2",
              "name": "E-commerce"
          },
          "children": {
              "61": {
                  "category": {
                      "id": "61", // &lt;&lt;&lt; I need to retrieve THIS
                      "pid": "28",
                      "com_id": "2",
                      "route_id": "52",
                      "excerpt": "",
                      "ordering": "1",
                      "title": "Ubor na hlavu",
                      "slug": "ubor-na-hlavu1",
                      "description": "&lt;p&gt;Capice, Klobuky, Panamy, Baretky, Prilby, Kukly&lt;br&gt;&lt;/p&gt;",
                      "image": "clothes-head.png",
                      "seo_title": "",
                      "meta": ""
                  },
                  "compname": {
                      "id": "2",
                      "name": "E-commerce"
                  },
                  "children": [ ]
              },
              "30": {
                  "category": {
                      "id": "30",
                      "pid": "28",
                      "com_id": "2",
                      "route_id": "53",
                      "excerpt": "",
                      "ordering": "2",
                      "title": "Bundy a vetrovky",
                      "slug": "bundy-a-vetrovky",
                      "description": "",
                      "image": "",
                      "seo_title": "",
                      "meta": ""
                  },
                  "compname": {
                      "id": "2",
                      "name": "E-commerce"
                  },
                  "children": {
                      "34": {
                          "category": {
                              "id": "34",
                              "pid": "30",
                              "com_id": "2",
                              "route_id": "0",
                              "excerpt": "",
                              "ordering": "0",
                              "title": "Letne",
                              "slug": "letne",
                              "description": "",
                              "image": "",
                              "seo_title": "",
                              "meta": ""
                          },
                          "compname": {
                              "id": "2",
                              "name": "E-commerce"
                          },
                          "children": [ ]
                      },
                      "35": {
                          "category": {
                              "id": "35",
                              "pid": "30",
                              "com_id": "2",
                              "route_id": "0",
                              "excerpt": "",
                              "ordering": "0",
                              "title": "Vsesezonne",
                              "slug": "vsesezonne",
                              "description": "",
                              "image": "",
                              "seo_title": "",
                              "meta": ""
                          },
                          "compname": {
                              "id": "2",
                              "name": "E-commerce"
                          },
                          "children": [ ]
                      },
                      "33": {
                          "category": {
                              "id": "33",
                              "pid": "30",
                              "com_id": "2",
                              "route_id": "0",
                              "excerpt": "",
                              "ordering": "0",
                              "title": "Zimne",
                              "slug": "zimne",
                              "description": "",
                              "image": "",
                              "seo_title": "",
                              "meta": ""
                          },
                          "compname": {
                              "id": "2",
                              "name": "E-commerce"
                          },
                          "children": [ ]
                      }
                  }
              },
              "31": {
                  "category": {
                      "id": "31",
                      "pid": "28",
                      "com_id": "2",
                      "route_id": "54",
                      "excerpt": "",
                      "ordering": "3",
                      "title": "Nohavice a teplaky",
                      "slug": "nohavice-a-teplaky",
                      "description": "",
                      "image": "",
                      "seo_title": "",
                      "meta": ""
                  },
                  "compname": {
                      "id": "2",
                      "name": "E-commerce"
                  },
                  "children": [ ]
              },
              "32": {
                  "category": {
                      "id": "32",
                      "pid": "28",
                      "com_id": "2",
                      "route_id": "55",
                      "excerpt": "",
                      "ordering": "4",
                      "title": "Obuv",
                      "slug": "obuv",
                      "description": "",
                      "image": "",
                      "seo_title": "",
                      "meta": ""
                  },
                  "compname": {
                      "id": "2",
                      "name": "E-commerce"
                  },
                  "children": [ ]
              }
          }
      },
      "38": {
      ...
  }
  </code></pre>
  
  <p>Could someone suggest me the appropriate way to achieve the same <code>foreach</code> statement in jQuery pls?</p>