I have an array of objects in the following format:
{ "country": "India", "children": [ { "name": "Karnataka", "type": "State", "children": [ { "name": "", "type": "city" }, { "name": "Bangalore", "type": "city" }, { "name": "Mangalore", "type": "city" } ] }, { "name": "Kerala", "type": "State", "children": [ { "name": "", "type": "city" } ] }, { "name": "Maharashtra", "type": "State", "children": [ { "name": "Mumbai", "type": "city" }, { "name": "Pune", "type": "city" } ] } ] }
Each object has a child element that contains the details of the element. I need to recursively iterate over a json object and delete all nodes whose name empty string to the root. For the above json format, the output should look like this:
{ "country": "India", "children": [ { "name": "Karnataka", "type": "State", "children": [ { "name": "Bangalore", "type": "city" }, { "name": "Mangalore", "type": "city" } ] }, { "name": "Kerala", "type": "State", "children": [ ] }, { "name": "Maharastra", "type": "State", "children": [ { "name": "Mumbai", "type": "city" }, { "name": "Pune", "type": "city" } ] } ] }
How to do it in javascript, recursive use of Underscorejs.