Reputation: 5056
So, this is my function that deletes empty and null properties from a JSON object in Javascript. I need the function to delete also empty nested objects but its not doing it right now. I have tried but failed several times to modify this function ( I got this from an old post in this site ).
Can someone help me with this?
function clean_object(test, recurse) {
for (var i in test) {
if (test[i] === null || test[i] == "" ) {
delete test[i];
} else if (recurse && typeof test[i] === 'object' ) {
clean_object(test[i], recurse);
}
}
}
{ "data.openstack.public_ipv4": "falseip" }
{"data":{"openstack":{}}}
{}
Thanks in advance!
Upvotes: 0
Views: 1480
Reputation: 13376
With the first development step as provided as a partial approach I just wanted to make the example(s) work in a way that it clears all of an objects null
values and zero-length string-values ...
function clearEmptyValuesRecursively(obj) {
if (Array.isArray(obj)) {
obj.forEach(function (item) {
clearEmptyValuesRecursively(item);
});
} else {
Object.keys(obj).forEach(function (key) {
var value = obj[key];
if ((value === null) || (value === '')) {
delete obj[key];
} else if (typeof value !== 'string') {
clearEmptyValuesRecursively(value);
}
});
}
return obj;
}
var data = {
"data": {
"openstack": {},
"fullstack": "fullstack",
"emptyString": ""
},
"emptyData": null
};
console.log('data - before : ', JSON.stringify(data));
clearEmptyValuesRecursively(data);
console.log('data - after : ', JSON.stringify(data));
data = {
"data": {
"openstack": {}
}
};
console.log('data - before : ', JSON.stringify(data));
clearEmptyValuesRecursively(data);
console.log('data - after : ', JSON.stringify(data));
.as-console-wrapper { max-height: 100%!important; top: 0; }
... within the second step I kind of recycled the above approach. This time the recursively working function was build for mainly clearing empty main (data) structures like {}
and []
, but it also takes care of deleting empty values as already shown with the first approach. Altogether this also is what the OP did ask for ...
function clearEmptyStructuresRecursively(obj) {
function isEmptyStructure(type) {
return ((Object.keys(type).length === 0) || (Array.isArray(type) && (type.length === 0)));
}
function isEmptyValue(type) {
return ((type == null) || (type === '')); // undefined or null or zero length string value.
}
if (Array.isArray(obj)) {
obj.forEach(function (item) {
clearEmptyStructuresRecursively(item);
});
} else if (obj && (typeof obj !== 'string')) {
Object.keys(obj).forEach(function (key) {
var value = obj[key];
if (isEmptyValue(value) || isEmptyStructure(value)) {
delete obj[key]; // ... delete ... and step into recursion ...
clearEmptyStructuresRecursively(obj);
}
clearEmptyStructuresRecursively(value);
});
Object.keys(obj).forEach(function (key) {
var value = obj[key];
if (isEmptyValue(value) || isEmptyStructure(value)) {
delete obj[key]; // ... final delete.
}
});
}
return obj;
}
var data = {
"data": {
"openstack": {}
}
};
console.log('data - before : ', JSON.stringify(data));
clearEmptyStructuresRecursively(data);
console.log('data - after : ', JSON.stringify(data));
data = {
"data": {
"openstack": {},
"fullstack": "fullstack",
"emptyString": ""
},
"emptyData": null
};
console.log('data - before : ', JSON.stringify(data));
clearEmptyStructuresRecursively(data);
console.log('data - after : ', JSON.stringify(data));
.as-console-wrapper { max-height: 100%!important; top: 0; }
Upvotes: 1