Skip to content

Instantly share code, notes, and snippets.

@AliBahaari
Created January 12, 2023 19:02
Show Gist options
  • Save AliBahaari/e73b0ed3aa483c916238259ca2e1abd7 to your computer and use it in GitHub Desktop.
Save AliBahaari/e73b0ed3aa483c916238259ca2e1abd7 to your computer and use it in GitHub Desktop.
Remove object properties which have `undefined` or `null` values.
interface IRemoveEmptyProperties {
[index: string]: any;
}
const removeEmptyProperties = <T extends IRemoveEmptyProperties = {}>(
param: T
) => {
let newObject: IRemoveEmptyProperties = {};
Object.keys(param).forEach((key: any) => {
if (param[key]) {
newObject[key] = param[key];
}
});
return newObject;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment