I have an array of objects, that can either have direct or nested values. The goal is to remove all empty fields.
For exemple:
const todos = [ {}, { not: {} } ]
// expected output: []
const todos2 = [ {}, { not: {countries: ["uk", "us"]} } ]
// expected output: [{ not: {countries: ["uk", "us"]} }]
I've tried to filter the array with Object.values.length, it when a nested value is an empty object, it doesn't work anymore. Would someone know how to do it?
EDIT: So I've came up with my own solution which is a bit simpler from what I've read here:
function foo(todos){
todos.map((todo,i)=> {
if(!Object.keys(todo).length){
return todos.splice(i, 1)
}
if(Object.keys(todo).length){
const key = Object.keys(todo) + ""
return !Object.values(todo[key]).length && todos.splice(i, 1)
}
return todo
})
return todos.filter(c=> Object.keys(c).length)
}