Created
May 10, 2017 15:20
-
-
Save burka/9dbf476781c5d0078a6ccb274cdd0da0 to your computer and use it in GitHub Desktop.
Remove null properties from javascript objects
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
"use strict"; | |
export default function removeNullProperties(obj) { | |
Object.keys(obj).forEach(key => { | |
let value = obj[key]; | |
let hasProperties = value && Object.keys(value).length > 0; | |
if (value === null) { | |
delete obj[key]; | |
} | |
else if ((typeof value !== "string") && hasProperties) { | |
removeNullProperties(value); | |
} | |
}); | |
return obj; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment