CARVIEW |
Select Language
HTTP/2 200
accept-ranges: bytes
age: 0
cache-control: public,max-age=0,must-revalidate
cache-status: "Netlify Edge"; fwd=miss
content-encoding: gzip
content-type: text/html; charset=UTF-8
date: Wed, 15 Oct 2025 01:29:57 GMT
etag: "d7377b111aa59554cbfa1afbe6c7ba6a-ssl-df"
server: Netlify
strict-transport-security: max-age=31536000
vary: Accept-Encoding
x-nf-request-id: 01K7JQSYXYF6ZCVJQQ3GSJB47J
How to Remove a null from an Object with Lodash - Mastering JS
How to Remove a null from an Object with Lodash
Jun 8, 2022
To remove a null
from an object with lodash, you can use the omitBy()
function.
const _ = require('lodash');
const obj = {a: null, b: 'Hello', c: 3, d: undefined};
const result = _.omitBy(obj, v => v === null); // {b: 'Hello', c: 3, d: undefined}
If you want to remove both null
and undefined
, you can use .isNil
or non-strict equality.
const _ = require('lodash');
const obj = {a: null, b: 'Hello', c: 3, d: undefined};
const result = _.omitBy(obj, _.isNil); // {b: 'Hello', c: 3}
const other = _.omitBy(obj, v => v == null); // {b: 'Hello', c: 3}
Using Vanilla JavaScript
You can use vanilla JavaScript to remove null
s from objects using Object.entries()
and Array filter()
.
However, the syntax is a bit messy.
Lodash omitBy()
is cleaner.
const obj = {a: null, b: 'Hello', c: 3, d: undefined, e: null};
Object.fromEntries(Object.entries(obj).filter(([key, value]) => value !== null)); // { b: "Hello", c: 3, d: undefined }
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!