Skip to content

Instantly share code, notes, and snippets.

@okian
Created November 1, 2017 12:24
Show Gist options
  • Save okian/5f155f59c4444fb336edd6f1c353897a to your computer and use it in GitHub Desktop.
Save okian/5f155f59c4444fb336edd6f1c353897a to your computer and use it in GitHub Desktop.
small function for clean objects
function reduce(x) {
var res = {}
var o;
for (o in x) {
if (!x.hasOwnProperty(o)){
continue;
}
if (Array.isArray(x[o])) {
var t = ac(x[o])
if (t.length>0){
res[o] = t
}
continue;
}
if (typeof x[o] === 'object' && x[o]) {
var t = reduce(x[o])
if (Object.keys(t).length!=0 ) {
res[o] = t
}
continue;
}
if (x[o]) {
res[o] = x[o]
}
}
return res
}
function ac(x) {
return x.map(c=> {
if (Array.isArray(c))
return ac(c)
if (typeof c === 'object' && c)
return reduce(c)
return c
}).filter(c=>!!c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment