I've got a data array:
const data = [{
name: 'South America',
locations: [{
name: 'Argentina',
locations: [{
name: 'Buenos Aires'
}, {}]
}]
}, {
name: 'Europe',
locations: [{
name: 'Spain',
locations: [{}]
}]
}, {
name: 'Asia',
locations: [{}]
}]
I'd like to remove any empty objects on any level (even if it's really deep). Returning as the example below:
[{
name: 'South America',
locations: [{
name: 'Argentina',
locations: [{
name: 'Buenos Aires'
}]
}]
}, {
name: 'Europe',
locations: [{
name: 'Spain',
locations: []
}]
}, {
name: 'Asia',
locations: []
}]
The key locations
could be anything else.
My solution is just partial as it only deletes the first level.
FAILED ATTEMPT
const result = data.filter(value => Object.keys(value).length !== 0)
I'd like this to be as dynamic as possible, without having to specify how nested will it be.