I am using angular 1.6.
I need remove some properties of a javascript object if they are null, empty or undefined.
In my case, i have two empty arrays 'byweekday" and "bymonth" and I am trying to delete them from object, without success.
What am I doing wrong?
//markup
<button class="btn btn-xs btn-success" type="button"
ng-click="makecalc()">
Calculate
</button>
//code
//angular controller
$scope.repeticao={"wkst":1,"freq":{"id":0,"name":"Anualmente"},"byweekday":
[],"bymonth":[]};
function removeEmpty(obj) {
Object.keys(obj).forEach(function(key) {
if (obj[key] && typeof obj[key] === 'object') removeEmpty(obj[key])
else if (obj[key] === null || obj[key] === undefined) delete obj[key]
});
return obj;
};
$scope.makecalc=function(){
//debugger;
var key, value, date;
var values = angular.copy($scope.repeticao);
values=removeEmpty(values);
console.log(JSON.stringify(values));
}
nullorundefinednot an empty array.