I have an array of objects in the below 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"
}
]
}
]
}
Every object has a children element which contains the details of the element. I need to recursively iterate through the json object and remove all the nodes whose name
is empty string up till the root. For the above json format, the output should be like below:
{
"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 this in javascript recursively using Underscorejs.
Array#map
..? – Rayon Dabre 1 hour ago