Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save UplandsDynamic/8210e210f3cfab0200450db5a91c5ccc to your computer and use it in GitHub Desktop.
Save UplandsDynamic/8210e210f3cfab0200450db5a91c5ccc to your computer and use it in GitHub Desktop.
Javascript (Typescript): Remove empty properties from object
deleteEmptyProps(obj: any): any {
// modifies passed obj in place, removing empty properties (inc. empty arrays)
return Object.keys(obj).forEach(k => {
if (!obj[k] || obj[k] === undefined ||
Array.isArray(obj[k]) && obj[k].length === 0) {
delete obj[k];
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment