Created
November 1, 2017 12:24
-
-
Save okian/5f155f59c4444fb336edd6f1c353897a to your computer and use it in GitHub Desktop.
small function for clean objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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