Utility remove nulls from object

October 19, 2023 · 1 min readLucian Ciobanu
Tags:JavaScript

Just wanted to take note of a cute little util I've been using lately to remove nulls from objects.

export function removeNulls(obj: any): any {
  const copy = {...obj};
  Object.keys.(copy).forEach(
    (key) => (copy[key] === null && delete copy[key]);
  return copy;
}

I just love how concise it is.

Hope this helps!

Cheers,
Lucian!

Picture of author

Web Development & Software Delivery