Skip to content

Instantly share code, notes, and snippets.

@danielo515
Created June 8, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielo515/c7826b0f2b9c743eb23f035f022f7680 to your computer and use it in GitHub Desktop.
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.
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