Last active
January 6, 2023 17:43
-
-
Save PaulTaykalo/9dba7f5c0e396b655ddea1c1ade7b40a to your computer and use it in GitHub Desktop.
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
const removeEmptyValues = val => { | |
const isInvalid = value => value === null || value === '' || value === false; | |
if (isInvalid(val)) return; | |
if (Array.isArray(val)) { | |
return val.map(removeEmptyValues).filter(val => val !== undefined); | |
} else if (typeof val === 'object') { | |
const entries = Object.entries(val).filter(([, value]) => !isInvalid(value)); | |
return Object.fromEntries(entries.map(([key, value]) => [key, removeEmptyValues(value)])); | |
} | |
return val; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment