Last active
July 12, 2018 19:20
-
-
Save qalqi/fea58983461af694867e669f2c70e81a to your computer and use it in GitHub Desktop.
Remove blank attributes from an Object in Javascript
This file contains 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
/** | |
* 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