9951 explained code solutions for 126 technologies


javascript-lodashHow can I use Lodash to remove empty keys from a JavaScript object?


Lodash provides a useful _.omitBy() method to remove empty keys from a JavaScript object. This method takes two arguments: the object and a function that returns true if the value should be omitted.

For example, the following code uses _.omitBy() to remove empty keys from a JavaScript object:

const obj = {
  a: 1,
  b: '',
  c: 0,
  d: false,
  e: null,
};

const result = _.omitBy(obj, _.isEmpty);

console.log(result);

Output example

{ a: 1, c: 0, d: false }

The code can be broken down into the following parts:

  1. const obj - This declares a variable obj and assigns to it an object with some keys and values.
  2. const result - This declares a variable result and assigns to it the result of calling _.omitBy() on obj.
  3. _.omitBy(obj, _.isEmpty) - This calls the _.omitBy() method on obj, passing in the _.isEmpty method as an argument. The _.isEmpty method returns true if the value is an empty string, null, undefined, or an empty array.
  4. console.log(result) - This logs the result of calling _.omitBy() to the console.

For more information, please see the Lodash documentation.

Edit this code on GitHub