close

How to remove undefined and null values from an object using lodash?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to remove undefined and null values from an object using lodash? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to remove undefined and null values from an object using lodash ?

  1. How to remove undefined and null values from an object using dash ?

    remove undefined and null values from an object using lodash To remove undefined, null, and empty string from object
    _.omitBy(object, (v) => _.isUndefined(v) || _.isNull(v) || v === '');

  2. remove undefined and null values from an object using lodash

    remove undefined and null values from an object using lodash To remove undefined, null, and empty string from object
    _.omitBy(object, (v) => _.isUndefined(v) || _.isNull(v) || v === '');

Method 1

The correct answer is:

_.omitBy({ a: null, b: 1, c: undefined, d: false }, _.isNil)

That results in:

{b: 1, d: false}

The alternative given here by other people:

_.pickBy({ a: null, b: 1, c: undefined, d: false }, _.identity);

Will remove also false values which is not desired here.

Method 2

To remove undefined, null, and empty string from object

_.omitBy(object, (v) => _.isUndefined(v) || _.isNull(v) || v === '');

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment