-2

I am new to javascript. I have a json data which I need to process using javascript.

    {
   "results":
   {
       "Karnataka":
       {
           "state_results":
           {
               "population":
               {
                   "totalRecords": 0,
                   "content":
                   [
                   ],
                   "id": null
               },
               "male_count":
               {
                   "totalRecords": 0,
                   "content":
                   [
                   ],
                   "id": null
               },
               "female_count":
               {
                   "totalRecords": 0,
                   "content":
                   [
                   ],
                   "id": null
               }
           },
           "district_results":
           {
               "links":
               [
               ],
               "totalRecords": 5,
               "content":
               [
                   {
                       "districtName": "Davanagere",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                   {
                       "districtName": "Tumkur",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                  {
                       "districtName": "Dharwar",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                  {
                       "districtName": "Mangalore",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                  {
                       "districtName": "Bagalkot",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
               ],
               "id": null
           }
       },
      "Kerala":
       {
           "state_results":
           {
               "population":
               {
                   "totalRecords": 0,
                   "content":
                   [
                   ],
                   "id": null
               },
               "male_count":
               {
                   "totalRecords": 0,
                   "content":
                   [
                   ],
                   "id": null
               },
               "female_count":
               {
                   "totalRecords": 0,
                   "content":
                   [
                   ],
                   "id": null
               }
           },
           "district_results":
           {
               "links":
               [
               ],
               "totalRecords": 5,
               "content":
               [
                   {
                       "districtName": "Davanagere",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                   {
                       "districtName": "Tumkur",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                  {
                       "districtName": "Dharwar",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                  {
                       "districtName": "Mangalore",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
                  {
                       "districtName": "Bagalkot",
                       "population": 1232323213213,
                       "area": "123123123 Sq Kms",

                   },
               ],
               "id": null
           }
       }
   }
}

I need to write a recursive function to traverse through the json and get the inner objects which contain the content section which has data. How do I write a recursive function using javascript?

zilcuanu
  • 3,451
  • 8
  • 52
  • 105
  • 1. What exactly you need as the output? 2. What have you tried till now? 3. Did you google for examples of JavaScript and recursion? – thefourtheye Jun 04 '14 at 05:52
  • `JSON.parse(myJsonString)` – Patrick Gunderson Jun 04 '14 at 05:52
  • Usually you make the function call itself, perhaps with slightly different parameters. Or perhaps as one branch of an if-else statement. It is done this way in C, C++, Fortran, Python, Perl, Java, any language that supports recursion, really. – Paul Jun 04 '14 at 05:52
  • This question appears to be off-topic because it lacks effort from OP. – thefourtheye Jun 04 '14 at 05:52
  • [**How to create recursive function in JavaScript**](http://msdn.microsoft.com/en-us/library/ie/wwbyhkx4(v=vs.94).aspx) – Stuart Jun 04 '14 at 05:53
  • 1
    Why would you need a recursive function here? The data does not seem to be arbitrarily nested. [Just loop over it!](http://stackoverflow.com/q/11922383/1048572) – Bergi Jun 04 '14 at 05:55

1 Answers1

1
// requires jquery
var contents = [];
var get_content = function(e) {
  $.each(e, function(k,v) {
    if (v.hasOwnProperty("content")) {
      contents.append(v);
    } else {
       get_content(v);
    }
  }
}
Fabricator
  • 12,722
  • 2
  • 27
  • 40