remove null values from object javascript code example
Example 1: javascript remove empty object items
var test = {
test1: null,
test2: 'somestring',
test3: 3,
}
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === null || obj[propName] === undefined) {
delete obj[propName];
}
}
return obj
}
console.log(test);
console.log(clean(test));
Example 2: nodejs remove null from object
for(var key in req.body){
if(req.body.hasOwnProperty(key)){
if(req.body[key] == null) delete req.body[key];
}
}