Skip to content

Instantly share code, notes, and snippets.

@PaulTaykalo
Last active January 6, 2023 17:43
Show Gist options
  • Save PaulTaykalo/9dba7f5c0e396b655ddea1c1ade7b40a to your computer and use it in GitHub Desktop.
Save PaulTaykalo/9dba7f5c0e396b655ddea1c1ade7b40a to your computer and use it in GitHub Desktop.
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