i have an array of objects that looks like: (obfuscated values)
[
{
"title": "l",
"data": [
{
"id": "x",
"bodyPart": "x",
"equipment": "x",
"gifUrl": "h",
"name": "x",
"target": "x",
"broad_target": "x",
"ppl": "p",
"thumbnail": "h",
"heatmap_target": "u"
}
]
},
{}
]
if you notice, there is a blank {}.
my attempt to remove these is:
const cleanEmpty = obj => Object.entries(output)
.map(([k,v])=>[k,v && typeof v === "object" ? cleanEmpty(v) : v])
.reduce((a,[k,v]) => (v == null ? a : (a[k]=v, a)), {});
this does not work.
>Solution :
you can use Array.filter to filter the blank objects like this :
const arr = [
{
foo: "bar"
},
{}
]
const newArr = arr.filter(obj => Object.keys(obj).length >= 1)
console.log(newArr) //[{foo: "bar"}]