Skip to content

Instantly share code, notes, and snippets.

@AndrewAllison
Created May 12, 2016 09:40
Show Gist options
  • Save AndrewAllison/c5d948b4a1d71a777788743a81bd4433 to your computer and use it in GitHub Desktop.
Save AndrewAllison/c5d948b4a1d71a777788743a81bd4433 to your computer and use it in GitHub Desktop.
Removing nulls from a Javascript object
function removeNullIn(prop, obj) {
console.info('We dont want nulls..', prop, obj);
var pr = obj[prop];
if (pr === null || pr === undefined) {
delete obj[prop];
}
else if (typeof pr === 'object') {
for (var i in pr) {
removeNullIn(i, pr)
}
};
}
function removeNull(obj) {
for (var i in obj) {
removeNullIn(i, obj);
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment