Skip to content

Instantly share code, notes, and snippets.

@smitroshin
Created January 20, 2021 00:16
Show Gist options
  • Save smitroshin/0035c626db7a26715a5b1ffa074e3c07 to your computer and use it in GitHub Desktop.
Save smitroshin/0035c626db7a26715a5b1ffa074e3c07 to your computer and use it in GitHub Desktop.
Clear object from empty values (recursively)
/**
* 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