Skip to content

Instantly share code, notes, and snippets.

@burka
Created May 10, 2017 15:20
Show Gist options
  • Save burka/9dbf476781c5d0078a6ccb274cdd0da0 to your computer and use it in GitHub Desktop.
Save burka/9dbf476781c5d0078a6ccb274cdd0da0 to your computer and use it in GitHub Desktop.
Remove null properties from javascript objects
"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