Skip to content

Instantly share code, notes, and snippets.

@azimidev
Created October 15, 2021 12:41
Show Gist options
  • Save azimidev/ad632fb406b2ce50a8b305a8628280af to your computer and use it in GitHub Desktop.
Save azimidev/ad632fb406b2ce50a8b305a8628280af to your computer and use it in GitHub Desktop.
CleanObject
/**
* Goes deep recursive through JSON object,
* if the children is only object and has less than 50 children {node},
* adds it to the new result otherwise return the child.
*
* @param obj
* @param node 50
* @returns {{}|*}
*/
function cleanObject(obj, node = 50) {
if (typeof obj === 'object' && !Array.isArray(obj)) {
const result = {};
Object.keys(obj).some(function(key) {
if (obj[key]) {
if (typeof obj[key] !== 'object') {
result[key] = obj[key];
}
if (typeof obj[key] === 'object' && Object.keys(obj[key]).length <= node) {
result[key] = cleanObject(obj[key], node);
}
} else {
result[key] = null;
}
});
return result;
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment