Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save qalqi/fea58983461af694867e669f2c70e81a to your computer and use it in GitHub Desktop.
Save qalqi/fea58983461af694867e669f2c70e81a to your computer and use it in GitHub Desktop.
Remove blank attributes from an Object in Javascript
/**
* Delete all null (or undefined) properties from an object.
* Set 'recurse' to true if you also want to delete properties in nested objects.
* Source: https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript
*/
function delete_null_properties(test, recurse) {
for (var i in test) {
if (test[i] === null) {
delete test[i];
} else if (recurse && typeof test[i] === 'object') {
delete_null_properties(test[i], recurse);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment