Created
October 15, 2021 12:41
-
-
Save azimidev/ad632fb406b2ce50a8b305a8628280af to your computer and use it in GitHub Desktop.
CleanObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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