Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Last active August 4, 2022 06:32
Show Gist options
  • Save sethdavis512/3403b9f34ee0caf1e9d332cdea640249 to your computer and use it in GitHub Desktop.
Save sethdavis512/3403b9f34ee0caf1e9d332cdea640249 to your computer and use it in GitHub Desktop.
// From https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript
const removeEmpty = (obj) => {
return Object.keys(obj)
.filter(k => obj[k] !== null && obj[k] !== undefined && obj[k] !== '')
.reduce((newObj, k) => {
return typeof obj[k] === 'object' ?
Object.assign(newObj, {[k]: removeEmpty(obj[k])}) : // Recurse.
Object.assign(newObj, {[k]: obj[k]}); // Copy value.
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment