Created
January 20, 2021 00:16
-
-
Save smitroshin/0035c626db7a26715a5b1ffa074e3c07 to your computer and use it in GitHub Desktop.
Clear object from empty values (recursively)
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
/** | |
* Clear object from empty values (recursively) | |
* Source: https://stackoverflow.com/a/38340374 | |
* | |
* @param {object} obj | |
* @returns {object} | |
*/ | |
const objectRemoveEmpty = (obj) => { | |
const newObj = {}; | |
Object.keys(obj).forEach((key) => { | |
if (obj[key] === Object(obj[key])) | |
newObj[key] = objectRemoveEmpty(obj[key]); | |
else if (!!obj[key] || obj[key] === 0) newObj[key] = obj[key]; | |
}); | |
return newObj; | |
}; | |
export default objectRemoveEmpty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment