Created
June 8, 2017 16:28
-
-
Save danielo515/c7826b0f2b9c743eb23f035f022f7680 to your computer and use it in GitHub Desktop.
A function that deletes empty object properties (null or undefined) and also deletes empty objects recursively.
This file contains hidden or 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 clearEmpties(o) { | |
for (const k in o) { | |
if (o[k] == null) { | |
delete o[k]; | |
continue; | |
} | |
if (typeof o[k] !== 'object') { | |
continue; | |
} | |
clearEmpties(o[k]); | |
if (Object.keys(o[k]).length === 0) { | |
delete o[k]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment